SpringBoot没有找到处理程序方法,第二个请求以后

时间:2017-07-29 02:37:21

标签: spring spring-boot spring-data-mongodb

我的应用程序中的一个端点仅在我第一次调用它时才起作用,但之后每次都失败并出现错误:

响应:

{
    "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=[org.springframework.web.servlet.resource.PathResourceResolver@74455848]]] 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.springframework.http.converter.json.MappingJackson2HttpMessageConverter@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: hello.update.MatchUpdate@54045f1c, links: [<http://localhost:8080/updates/597ad268c843e7286ab1d059>;rel="self", <http://localhost:8080/updates/597ad268c843e7286ab1d059>;rel="matchUpdate"] }, Resource { content: hello.update.MatchUpdate@48f57e91, 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.RepositoryRestMvcConfiguration$ResourceSupportHttpMessageConverter@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
  • Controller与我的其他控制器一起位于顶层,因此Spring应该能够找到它
  • Application类使用@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);
    }
}

1 个答案:

答案 0 :(得分:0)

由于找不到页面而在项目控制器中发生

404错误,因此未使用正确的路径进行映射,您需要更正错误。 您使用的是@RepositoryRestController,具体取决于您是否执行特定于实体的操作。在这种情况下,您需要手动创建端点。您可以在下面参考RepositoryRestController的参考链接。

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

您也可以使用

@RestController
@RequestMapping("/controller")

映射控制器路径。