2017-07-29 349 views
0

一个在我的应用程序的端点只能我第一次调用它,但与错误后未能每次:SpringBoot没有找到处理方法,开始第二个请求

响应:

{ 
    "timestamp": 1501295150391, 
    "status": 404, 
    "error": "Not Found", 
    "message": "No message available", 
    "path": "/updates" 
} 

日志:

2017-07-28 19:25:50.386 DEBUG 76613 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet  : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/updates] 
2017-07-28 19:25:50.386 DEBUG 76613 --- [nio-8080-exec-2] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /updates 
2017-07-28 19:25:50.386 DEBUG 76613 --- [nio-8080-exec-2] s.w.s.m.m.a.RequestMappingHandlerMapping : Did not find handler method for [/updates] 
2017-07-28 19:25:50.387 DEBUG 76613 --- [nio-8080-exec-2] o.s.w.s.handler.SimpleUrlHandlerMapping : Matching patterns for request [/updates] are [/**] 
2017-07-28 19:25:50.387 DEBUG 76613 --- [nio-8080-exec-2] o.s.w.s.handler.SimpleUrlHandlerMapping : URI Template variables for request [/updates] are {} 
2017-07-28 19:25:50.388 DEBUG 76613 --- [nio-8080-exec-2] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapping [/updates] 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]455848]]] and 1 interceptor 
2017-07-28 19:25:50.389 DEBUG 76613 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet  : Last-Modified value for [/updates] is: -1 
2017-07-28 19:25:50.389 DEBUG 76613 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet  : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling 
2017-07-28 19:25:50.389 DEBUG 76613 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet  : Successfully completed request 
2017-07-28 19:25:50.390 DEBUG 76613 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet  : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/error] 
2017-07-28 19:25:50.390 DEBUG 76613 --- [nio-8080-exec-2] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /error 
2017-07-28 19:25:50.390 DEBUG 76613 --- [nio-8080-exec-2] s.w.s.m.m.a.RequestMappingHandlerMapping : Returning handler method [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)] 
2017-07-28 19:25:50.390 DEBUG 76613 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet  : Last-Modified value for [/error] is: -1 
2017-07-28 19:25:50.391 DEBUG 76613 --- [nio-8080-exec-2] o.s.w.s.m.m.a.HttpEntityMethodProcessor : Written [{timestamp=Fri Jul 28 19:25:50 PDT 2017, status=404, error=Not Found, message=No message available, path=/updates}] as "application/hal+json" using [org.springfr[email protected]e4487af] 
2017-07-28 19:25:50.392 DEBUG 76613 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet  : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling 
2017-07-28 19:25:50.392 DEBUG 76613 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet  : Successfully completed request 

然而,它作品第一次它被称为,与日志显示:

2017-07-28 19:55:41.085 DEBUG 77262 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet  : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/updates] 
2017-07-28 19:55:41.085 DEBUG 77262 --- [nio-8080-exec-2] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /updates 
2017-07-28 19:55:41.085 DEBUG 77262 --- [nio-8080-exec-2] s.w.s.m.m.a.RequestMappingHandlerMapping : Did not find handler method for [/updates] 
2017-07-28 19:55:41.089 DEBUG 77262 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet  : Last-Modified value for [/updates] is: -1 
2017-07-28 19:55:41.135 DEBUG 77262 --- [nio-8080-exec-2] m.m.a.RequestResponseBodyMethodProcessor : Written [PagedResource { content: [Resource { content: [email protected], links: [<http://localhost:8080/updates/597ad268c843e7286ab1d059>;rel="self", <http://localhost:8080/updates/597ad268c843e7286ab1d059>;rel="matchUpdate"] }, Resource { content: [email protected], links: [<http://localhost:8080/updates/597ad26ac843e7286ab1d05a>;rel="self", <http://localhost:8080/updates/597ad26ac843e7286ab1d05a>;rel="matchUpdate"] }], metadata: Metadata { number: 0, total pages: 1, total elements: 2, size: 30 }, links: [<http://localhost:8080/updates>;rel="self", <http://localhost:8080/profile/updates>;rel="profile", <http://localhost:8080/updates/search>;rel="search"] }] as "application/hal+json" using [org.springframework.data.rest.webmvc.config.Rep[email protected]c86b9e3] 
2017-07-28 19:55:41.135 DEBUG 77262 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet  : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling 
2017-07-28 19:55:41.136 DEBUG 77262 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet  : Successfully completed request 
  • 控制器在与我的其他控制器沿顶层,所以春天应该能够找到它
  • 的应用类标注有@ComponentScan(basePackageClasses = MatchUpdateController.class)尝试,并确保它位于它

相关的文件:

MatchUpdateRepository:

@RepositoryRestResource(collectionResourceRel = "updates", path = "updates") 
public interface MatchUpdateRepository extends MongoRepository<MatchUpdate, String>, MatchUpdateRepositoryCustom { 

    List<MatchUpdate> findByReceiverId(@Param("id") String id); 
} 

MatchUpdateController:

@RepositoryRestController 
public class MatchUpdateController { 

    private final MatchUpdateRepository repository; 

    @Autowired 
    public MatchUpdateController(MatchUpdateRepository repo) { 
     repository = repo; 
    } 

    @RequestMapping(value = "/updates", method = RequestMethod.POST) 
    public ResponseEntity<?> create(@RequestBody MatchUpdate update, UriComponentsBuilder builder, 
            @RequestHeader(value= HttpHeaders.AUTHORIZATION) String senderId) { 
     if (senderId == null) { 
      return ResponseEntity.badRequest().body("Authorization header required"); 
     } else { 
      return repository.createUpdate(update, senderId, builder); 
     } 
    } 

    @RequestMapping(value = "/updates/{id}/accept", method = RequestMethod.POST) 
    public ResponseEntity<?> accept(@PathVariable("id") String id, @RequestBody MatchUpdateResponse response, 
             @RequestHeader(value= HttpHeaders.AUTHORIZATION) String senderId) { 
     if (response == null) { 
      return ResponseEntity.badRequest().body("Request body cannot be null"); 
     } else { 
      return repository.acceptUpdate(response, id, senderId); 
     } 
    } 

    @RequestMapping(value = "/updates/{id}/decline", method = RequestMethod.POST) 
    public ResponseEntity<?> decline(@PathVariable("id") String id, @RequestBody MatchUpdateResponse response, 
             @RequestHeader(value= HttpHeaders.AUTHORIZATION) String senderId) { 
     if (response == null) { 
      return ResponseEntity.badRequest().body("Request body cannot be null"); 
     } else { 
      return repository.declineUpdate(response, id, senderId); 
     } 
    } 
} 

应用:由于页面没有找到发生

@SpringBootApplication 
@ComponentScan(basePackageClasses = MatchUpdateController.class) 
public class Application { 

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

回答

0

404错误,并在您的项目CON troller,它没有映射到正确的路径,你需要纠正它。 您正在使用@RepositoryRestController,具体取决于您是否执行特定于实体的操作。在这种情况下,您需要手动创建端点。你可以参考下面的参考链接RepositoryRestController

http://docs.spring.io/spring-data/rest/docs/current/reference/html/#customizing-sdr.overriding-sdr-response-handlers

你也可以使用

@RestController 
@RequestMapping("/controller") 

映射控制器路径。