2016-05-03 99 views
5

我試圖用Spring Boot實現簡單演示MVC應用程序,但執行應用程序時出現404錯誤。該URI是`http://localhost:8080/',它將顯示一個名爲circle的表中的所有行。Rest Controller無法識別Spring Boot App中的GET請求

  • 春季啓動:1.3.3.RELEASE
  • Java版本:1.8.0_65
  • 數據庫:Apache Derby的10.12.1.1

Maven的Java項目:

Maven Java Project Structure

Application.java

package com.nomad.dubbed.app; 

import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 

@SpringBootApplication 
public class Application { 

    public static void main(String[] args){ 
     SpringApplication.run(Application.class, args); 
    } 

} 

CircleController.java

package com.nomad.dubbed.controller; 

import java.util.List; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.RestController; 

import com.nomad.dubbed.dao.CircleService; 
import com.nomad.dubbed.model.Circle; 

@RestController 
@RequestMapping("/") 
public class CircleController { 
    @Autowired 
    private CircleService circleService; 

    @RequestMapping(method=RequestMethod.GET) 
    public List<Circle> getAll() { 
     return circleService.getAll(); 
    } 

} 

CircleRepository.java

package com.nomad.dubbed.dao; 

import org.springframework.data.jpa.repository.JpaRepository; 
import org.springframework.stereotype.Repository; 

import com.nomad.dubbed.model.Circle; 

@Repository 
public interface CircleRepository extends JpaRepository<Circle, Integer> { 

} 

CircleService.java

package com.nomad.dubbed.dao; 

import java.util.List; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Service; 
import org.springframework.transaction.annotation.Propagation; 
import org.springframework.transaction.annotation.Transactional; 

import com.nomad.dubbed.model.Circle; 

@Service 
public class CircleService { 
    @Autowired 
    private CircleRepository circleRepository; 

    @Transactional(propagation=Propagation.REQUIRED) 
    public List<Circle> getAll(){ 
     return circleRepository.findAll(); 
    } 

} 

Circle.java

package com.nomad.dubbed.model; 

import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 
import javax.persistence.Table; 

@Entity 
@Table(name="circle") 
public class Circle { 
    @Id 
    @GeneratedValue(strategy=GenerationType.IDENTITY) 
    private int id; 
    private String name; 

    public Circle(int id, String name) { 
     super(); 
     this.id = id; 
     this.name = name; 
    } 

    public int getId() { 
     return id; 
    } 
    public void setId(int id) { 
     this.id = id; 
    } 
    public String getName() { 
     return name; 
    } 
    public void setName(String name) { 
     this.name = name; 
    } 

} 

application.properties

spring.datasource.url=jdbc:derby://localhost:1527/db 
spring.datasource.driverClassName=org.apache.derby.jdbc.ClientDriver 

logging.level.org.springframework.web:DEBUG 
logging.level.org.hibernate:DEBUG 

的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>com.nomad.dubbed</groupId> 
    <artifactId>spring-boot-mvc</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 


    <properties> 
     <derby-client.version>10.11.1.1</derby-client.version> 
    </properties> 

    <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</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-web</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-actuator</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-remote-shell</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-data-jpa</artifactId> 
     </dependency> 

     <dependency> 
      <groupId>org.apache.derby</groupId> 
      <artifactId>derbyclient</artifactId> 
      <version>${derby-client.version}</version> 
     </dependency> 

     <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <scope>test</scope> 
     </dependency> 
    </dependencies> 
    <build> 
     <finalName>spring-boot-mvc</finalName> 
     <plugins> 
      <plugin> 
       <groupId>org.springframework.boot</groupId> 
       <artifactId>spring-boot-maven-plugin</artifactId> 
      </plugin> 
     </plugins> 
    </build> 

</project> 

數據庫是啓動和運行,有5個RO WS在表圈:

enter image description here

默認URI(/豆/健康..)工作正常,但實現的控制器無法識別。控制檯中沒有顯示錯誤,下面是發送請求後在控制檯中打印的日誌轉儲。

2016-05-03 14:17:26.594 DEBUG 659 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet  : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/] 
2016-05-03 14:17:26.596 DEBUG 659 --- [nio-8080-exec-3] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path/
2016-05-03 14:17:26.596 DEBUG 659 --- [nio-8080-exec-3] s.w.s.m.m.a.RequestMappingHandlerMapping : Did not find handler method for [/] 
2016-05-03 14:17:26.597 DEBUG 659 --- [nio-8080-exec-3] o.s.w.s.handler.SimpleUrlHandlerMapping : Matching patterns for request [/] are [/**] 
2016-05-03 14:17:26.597 DEBUG 659 --- [nio-8080-exec-3] o.s.w.s.handler.SimpleUrlHandlerMapping : URI Template variables for request [/] are {} 
2016-05-03 14:17:26.597 DEBUG 659 --- [nio-8080-exec-3] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapping [/] to HandlerExecutionChain with handler [ResourceHttpRequestHandler [locations=[ServletContext resource [/], class path resource [META-INF/resources/], class path resource [resources/], class path resource [static/], class path resource [public/]], resolvers=[[email protected]13019c]]] and 1 interceptor 
2016-05-03 14:17:26.597 DEBUG 659 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet  : Last-Modified value for [/] is: -1 
2016-05-03 14:17:26.597 DEBUG 659 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet  : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling 
2016-05-03 14:17:26.597 DEBUG 659 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet  : Successfully completed request 
2016-05-03 14:17:26.597 DEBUG 659 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet  : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/error] 
2016-05-03 14:17:26.600 DEBUG 659 --- [nio-8080-exec-3] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /error 
2016-05-03 14:17:26.600 DEBUG 659 --- [nio-8080-exec-3] s.w.s.m.m.a.RequestMappingHandlerMapping : Returning handler method [public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)] 
2016-05-03 14:17:26.600 DEBUG 659 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet  : Last-Modified value for [/error] is: -1 
2016-05-03 14:17:26.601 DEBUG 659 --- [nio-8080-exec-3] o.s.w.s.v.ContentNegotiatingViewResolver : Requested media types are [text/html, text/html;q=0.8] based on Accept header types and producible media types [text/html]) 
2016-05-03 14:17:26.601 DEBUG 659 --- [nio-8080-exec-3] o.s.w.s.v.ContentNegotiatingViewResolver : Returning [org.springfram[email protected]2f5f8d71] based on requested media type 'text/html' 
2016-05-03 14:17:26.601 DEBUG 659 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet  : Rendering view [org.springfram[email protected]2f5f8d71] in DispatcherServlet with name 'dispatcherServlet' 
2016-05-03 14:17:26.601 DEBUG 659 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet  : Successfully completed request 

Rest App Browser

+0

什麼是您的應用程序路徑? – ACV

+2

採取更小的步驟。首先讓getAll返回「hello」。 –

回答

11

使用不同的網址,您的控制器。 spring-boot中的「​​/」映射到位於META-INF/resources和src/main/resources/static /中的靜態資源。

編輯:上面忘了,做在你的應用類中的以下內容:

Application.java

package com.nomad.dubbed.app; 

import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 

@SpringBootApplication 
@ComponentScan("com.nomad.dubbed") 
public class Application { 

    public static void main(String[] args){ 
     SpringApplication.run(Application.class, args); 
    } 

} 

你的休息控制器不被彈簧靴組件掃描發現的。根據此文檔http://docs.spring.io/spring-boot/docs/current/reference/html/ ... spring掃描具有@SpringBootApplication註釋的類所在的包下面的包。您的控制器位於並行包裝中。

+0

@lume我將它改爲'/ circles',仍然是同樣的錯誤。 '@RequestMapping(值= 「/圈」,方法= RequestMethod.GET,產生= { 「應用程序/ JSON」, 「應用/ XML」}) \t @ResponseBody \t公共列表 GETALL(){ \t \t返回circleService.getAll(); \t}' – gkc123

+2

該死的,奇怪的。另一個想法是:在啓動時,spring-boot記錄所有映射的URL。你也可以發佈啓動日誌消息嗎? – lumue

+2

我想我發現你的問題:你的休息控制器沒有被spring-boots組件掃描發現。 根據此文檔https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-structuring-your-code.html春季掃描軟件包下面的包與類@SpringBootApplication註釋駐留。 您的控制器位於並行包裝中。 – lumue

0

你能嘗試添加@ResponseBody註釋

@RequestMapping(method=RequestMethod.GET) 
@ResponseBody 
    public List<Circle> getAll() { 
     return circleService.getAll(); 
    } 
+0

我按照你的建議修改了代碼,沒有運氣。我仍然沒有找到[/]'錯誤的處理方法。 @ACV應用程序路徑爲/ @ @RequestMapping(value =「/」,method = RequestMethod.GET,產生= {「application/json」,「application/xml」}) @ResponseBody \t public List getAll { \t \t return circleService.getAll(); \t}' – gkc123

+0

作爲評論sugest嘗試採取較小的步驟。返回一個字符串列表的工作? – jstuartmilne

0

我必須研究更多爲什麼spring-boot無法識別控制器與原始包裝結構。我把所有的java類都放到了一個包中,最後讓演示項目運行。

修改Java項目結構:

Modified Java Project Structure

的CircleController.java類也進行了改造。我沒有提及具體的請求方法method=RequestMethod.GET,從圓圈表中刪除了所有記錄。

package com.nomad.dubbed.app; 

import java.util.List; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.RestController; 

@RestController 
public class CircleController { 
    @Autowired 
    private CircleService circleService; 

    @RequestMapping(value="/circles", method=RequestMethod.GET) 
    public List<Circle> getAll() { 
     return circleService.getAll(); 
    } 

} 
0

我有同樣的問題,我加入@ComponentScan(basePackages = 「package.name」)在應用程序類。之後,我的休息控制器被確認。

package com.nomad.dubbed.app;

import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 

@SpringBootApplication 
@ComponentScan(basePackages = "com.spring.basepkg") 
public class Application { 

    public static void main(String[] args){ 
     SpringApplication.run(Application.class, args); 
    } 

}