[英]Java Spring Boot REST Service
在我問之前,為我的英語蹩腳道歉。
目前我使用Spring Boot
開發一些 REST Web 服務。 我使用 Maven、JPA 並使用 Postgresql 作為數據庫。 我的問題是,當我運行spring boot
,我看到沒有錯誤,它正常啟動; 但是當我在瀏覽器中嘗試時,出現錯誤:
在我的 eclipse 控制台中,我看到了這個類似錯誤的日志:
Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
這是我的項目結構:
pom.xml :
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
<relativePath />
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4-1201-jdbc41</version>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
<properties>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ejb-plugin</artifactId>
<configuration>
<ejbVersion>3.0</ejbVersion>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>
應用程序.java
package com.altoremittance.data;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jms.JndiConnectionFactoryAutoConfiguration;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@Configuration
@SpringBootApplication
@EnableJpaRepositories
@EnableAutoConfiguration
@ComponentScan("com.altoremittance.data.service")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
成員控制器.java
package com.altoremittance.data.controller;
import java.util.List;
import org.json.simple.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.altoremittance.service.MemberService;
import com.altoremittance.data.domain.Member;
@RestController
@RequestMapping("/member")
public class MemberController {
@Autowired
private MemberService memberService;
@RequestMapping(value="/addMember", method=RequestMethod.GET, produces="application/json")
public @ResponseBody String getAllMember(){
JSONObject jo = new JSONObject();
jo.put("err_code", "00");
return jo.toString();
}
}
MemberRepository.java
package com.altoremittance.service;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import com.altoremittance.data.domain.Member;
public interface MemberRepository extends JpaRepository<Member, Long> {
}
MemberService.java包 com.altoremittance.service;
import java.util.List;
import org.springframework.stereotype.Component;
import com.altoremittance.data.domain.Member;
public interface MemberService {
public List<Member> findAll();
}
MemberServiceImpl.java
package com.altoremittance.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.altoremittance.data.domain.Member;
@Service
@Transactional
public class MemberServiceImpl implements MemberService {
@Autowired
private MemberRepository memberRepo;
public List<Member> findAll(){
return memberRepo.findAll();
}
}
我看到了您的附加圖片,並且看到您正在嘗試訪問http://localhost:8090/member/getAllMember
...
如果您的控制器@RequestMapping
已像addMember
一樣定義,您為什么要嘗試訪問getAllMember
路徑?
嘗試訪問http://localhost:8090/member/addmember
URL 而不是之前的 URL。
希望這會有所幫助。
問候,
最后,我知道這個問題,我在我的Application.java 中更新
在@ComponentScan 中,我更改為@ComponentScan("com.altoremittance")。
我參考了這篇文章: 在此處輸入鏈接描述
謝謝大家。
將 ComponentScan 設置為@ComponentScan("com.altoremittance")
。
you are firing wrong url i.e
http://localhost:8090/member/getAllMember ( there is no "getAllMember" mapping on method level)
It should be
http://localhost:8090/member/addMember ( please see, you have mentioned "addMember" mapping on method level)
and also remove extra annotation from your code , as per your code not required all.
it is in proper package n all. Below code is sufficient to run your service.
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
OR,
@ComponentScan("com.altoremittance.*")
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.