2016-05-03 17 views
5

Sto cercando di implementare una semplice app demo MVC con Spring Boot ma ottengo 404 errori durante l'esecuzione dell'applicazione. L'uri è `http://localhost:8080/ 'che serve per visualizzare tutte le righe in una tabella chiamata circle.Rest Controller non riconosce la richiesta GET in Spring Boot App

  • Primavera Boot: 1.3.3.RELEASE
  • Java Version: 1.8.0_65
  • Database: Apache Derby 10.12.1.1

Maven Java del progetto:

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> 

Database è installato e funzionante, ci sono 5 ro ws nel cerchio tabella:

enter image description here

L'URI di default (/ fagioli,/salute ..) funziona bene, ma il controller implementato non viene riconosciuto. Non c'è alcun errore di tale visualizzazione nella console, sotto è il dump di registri stampati in console dopo che ho inviato la richiesta.

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

qual è il percorso dell'applicazione? – ACV

+2

Fai piccoli passi. Fai in modo che il getTutti ritorni "ciao" all'inizio. –

risposta

11

utilizzare un URL diverso per il controller. "/" in spring-boot esegue il mapping alle risorse statiche situate in META-INF/resources e src/main/resources/static /.

edit: dimenticare sopra e effettuare le seguenti operazioni nella classe di applicazione:

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); 
    } 

} 

il controller resto non viene scoperto dalla primavera-boots di scansione dei componenti. in base al documento http://docs.spring.io/spring-boot/docs/current/reference/html/ ... Spring analizza i pacchetti sotto il pacchetto in cui risiede la classe con l'annotazione @SpringBootApplication. il controller si trova in un pacchetto parallelo.

+0

@lume L'ho cambiato in '/ circles', ancora lo stesso errore. '@RequestMapping (value = "/ cerchi", method = RequestMethod.GET, produce = { "application/json", "application/xml"}) \t @ResponseBody \t Lista pubblico getAll() { \t \t ritorno circleService.getAll(); \t} ' – gkc123

+2

dannazione, strano. un altro pensiero: spring-boot registra tutti gli URL associati all'avvio. potresti pubblicare anche i messaggi del log di avvio? – lumue

+2

Penso di aver trovato il tuo problema: il tuo controller di riposo non viene scoperto dalla scansione dei componenti di spring-boots. in base a questo documento https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-structuring-your-code.html scansiona i pacchetti sotto il pacchetto in cui la classe con il L'annotazione @SpringBootApplication risiede. il controller si trova in un pacchetto parallelo. – lumue

0

Potrebbe provare ad aggiungere l'annotazione @ResponseBody

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

Ho modificato il codice come suggerito, senza fortuna. Ricevo ancora 'Non trovato il metodo di gestione per l'errore [/]'. percorso di applicazione @ACV è/'@RequestMapping (value = "/", method = RequestMethod.GET, produce = { "application/json", "application/xml"}) \t @ResponseBody \t Lista pubblico getAll() { \t \t return circleService.getAll(); \t} ' – gkc123

+0

Come commento sugest provare a fare piccoli passi. Funziona la restituzione di un elenco di stringhe? – jstuartmilne

0

Devo cercare di più perché spring - boot non è riuscito a riconoscere il controller con la struttura del pacchetto originale. Ho scaricato tutte le classi Java in un unico pacchetto e finalmente ho avviato il progetto demo.

Modificato Struttura Java del progetto:

Modified Java Project Structure

La classe CircleController.java è stato inoltre modificato. Ho tutti i record cancellati dalla tabella circolare senza menzionare il metodo di richiesta specifico, 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

Ho avuto lo stesso problema e ho aggiunto @ComponentScan (basePackages = "package.name") nella classe Application. Successivamente è stato riconosciuto il mio controller di riposo.

pacchetto 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); 
    } 

} 
Problemi correlati