Spring Boot:多个@RequestMapping方法不适用于嵌入式Tomcat

时间:2017-01-26 20:47:04

标签: java rest tomcat spring-boot

我遇到一个问题,即公共基本路径周围的多个@RequestMappings无法正常工作。

我已经定义了一个类级别映射,如下所示:

@RestController
@RequestMapping( "/we/just/want/to" )
public class DoitController

使用以下方法映射

@RequestMapping( path = "/doit", method = RequestMethod.GET )
public ResponseEntity<String> doitUsingQueryParams(
    @RequestParam( name = "abc", required = false ) String abc,
    @RequestParam( name = "def", required = false ) String def,
    HttpServletRequest request ) throws Exception
{
    ...
}

@RequestMapping( path = "/doit/{abc}", method = RequestMethod.GET )
public ResponseEntity<String> doitWithAbc(
        @PathVariable String abc,
        HttpServletRequest request ) throws Exception
{
    ...
}

@RequestMapping( path = "/doit/{abc}/{def}", method = RequestMethod.GET )
public ResponseEntity<String> doitWithAbcAndDef(
        @PathVariable String abc,
        @PathVariable String def,
        HttpServletRequest request ) throws Exception
{
    ...
}

执行具有以下路径之一的请求时

/we/just/want/to/doit/theabcvalue
/we/just/want/to/doit/theabcvalue/thedefvalue

找不到请求映射(访问日志显示请求返回404)。但是运行具有路径和查询字符串的请求如下

/we/just/want/to/doit?abc=theabcvalue 
/we/just/want/to/doit?abc=theabcvalue&def=thedefvalue
找到

并正确映射到指定的方法。

我对我正确完成的工作感到茫然,并非常感谢所提供的任何帮助。

更新 - 2017年1月27日星期五16:01:21 UTC

有趣的是,此代码仅在Unix服务器上作为可执行jar运行时才会出现问题。在我的本地Windows 7笔记本电脑上运行Eclipse时,每个方法都可以正常执行。以下是我们服务器的具体内容:

CentOS release 6.7 (Final)  
Linux <server_name_removed> 2.6.32-573.12.1.el6.x86_64 #1 SMP Tue Dec 15 21:19:08 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
java version "1.8.0_45"
Java(TM) SE Runtime Environment (build 1.8.0_45-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)

另请注意,请求映射确实出现在日志文件输出中:

15:53:04,548 t=1485532384 tn=548000000 pid=2520 h=xx.xx.xx.110 lynx f=org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping l=534 lvl=INFO : Mapped "{[/we/just/want/to/doit],methods=[GET]}" onto public org.springframework.http.ResponseEntity<java.lang.String> com.xxxxxx.lynx.DoitController.doitUsingQueryParams(java.lang.String,java.lang.String,javax.servlet.http.HttpServletRequest) throws java.lang.Exception
15:53:04,548 t=1485532384 tn=548000000 pid=2520 h=xx.xx.xx.110 lynx f=org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping l=534 lvl=INFO : Mapped "{[/we/just/want/to/doit/{abc}],methods=[GET]}" onto public org.springframework.http.ResponseEntity<java.lang.String> com.xxxxxx.lynx.DoitController.doitWithAbc(java.lang.String,javax.servlet.http.HttpServletRequest) throws java.lang.Exception
15:53:04,549 t=1485532384 tn=549000000 pid=2520 h=xx.xx.xx.110 lynx f=org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping l=534 lvl=INFO : Mapped "{[/we/just/want/to/doit/{abc}/{def}],methods=[GET]}" onto public org.springframework.http.ResponseEntity<java.lang.String> com.xxxxxx.lynx.DoitController.doitWithAbcAndDef(java.lang.String,java.lang.String,javax.servlet.http.HttpServletRequest) throws java.lang.Exception

0 个答案:

没有答案