Java 如何在SpringREST中同时使用@Pathvariable和@RequestParam

Java 如何在SpringREST中同时使用@Pathvariable和@RequestParam,java,spring,spring-boot,spring-rest,Java,Spring,Spring Boot,Spring Rest,我试图在spring boot中提供rest服务,以匹配如下请求: http://localhost:8080/customer/v1/user/1/purchase?productId=2 @GetMapping(value = "/customer/v1/user/{userId}/purchase?productId={productId}") public ResponseDto Test(@PathVariable("userId") String userId,@RequestPa

我试图在spring boot中提供rest服务,以匹配如下请求:

http://localhost:8080/customer/v1/user/1/purchase?productId=2
@GetMapping(value = "/customer/v1/user/{userId}/purchase?productId={productId}")
public ResponseDto Test(@PathVariable("userId") String userId,@RequestParam("productId") String productId ){
    return  new ResponseDto();
}
因此,我创建了如下服务:

http://localhost:8080/customer/v1/user/1/purchase?productId=2
@GetMapping(value = "/customer/v1/user/{userId}/purchase?productId={productId}")
public ResponseDto Test(@PathVariable("userId") String userId,@RequestParam("productId") String productId ){
    return  new ResponseDto();
}
但它似乎与请求不匹配,我得到以下错误:

DispatcherServlet with name 'dispatcherServlet' processing GET request for [/customer/v1/user/1/purchase]
Looking up handler method for path /customer/v1/user/1/purchase
Did not find handler method for [/customer/v1/user/1/purchase]
Matching patterns for request [/customer/v1/user/1/purchase] are [/**]
URI Template variables for request [/customer/v1/user/1/purchase] are {}
Mapping [/customer/v1/user/1/purchase] to HandlerExecutionChain with handler [ResourceHttpRequestHandler [locations=[class path resource [META-INF/resources/], class path resource [resources/], class path resource [static/], class path resource [public/], ServletContext resource [/]],
Last-Modified value for [/customer/v1/user/1/purchase] is: -1
Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
Successfully completed request
DispatcherServlet with name 'dispatcherServlet' processing GET request for [/error]
Looking up handler method for path /error
Returning handler method [public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)]
Last-Modified value for [/error] is: -1
Written [{timestamp=Mon Oct 29 17:52:20 IRST 2018, status=404, error=Not Found, message=No message available, path=/customer/v1/user/1/purchase}] as "application/json" using [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@bb8ead8]
Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
Successfully completed request
名为“DispatcherServlet”的DispatcherServlet正在处理[/customer/v1/user/1/purchase]的GET请求 查找路径/customer/v1/user/1/purchase的处理程序方法 未找到[/customer/v1/user/1/purchase]的处理程序方法 请求[/customer/v1/user/1/purchase]的匹配模式为[/**] 请求[/customer/v1/user/1/purchase]的URI模板变量为{} 将[/customer/v1/user/1/purchase]映射到HandlerExecutionChain,并使用handler[ResourceHttpRequestHandler[locations=[class path resource[META-INF/resources/],class path resource[resources/],class path resource[static/],class path resource[public/],ServletContext resource[/], [/customer/v1/user/1/purchase]的上次修改值为:-1 返回给名为“DispatcherServlet”的DispatcherServlet的Null ModelAndView:假设HandlerAdapter已完成请求处理 已成功完成请求 名为“DispatcherServlet”的DispatcherServlet正在处理获取请求[/error] 查找路径/错误的处理程序方法 返回处理程序方法[public org.springframework.http.ResponseEntity org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)] [/error]的上次修改值为:-1 使用[org.springframework.http.converter.json]将[{timestamp=Mon-Oct 29 17:52:20-IRST 2018,status=404,error=Not-Found,message=No-message,path=/customer/v1/user/1/purchase}]写成“application/json”。MappingJackson2HttpMessageConverter@bb8ead8] 返回给名为“DispatcherServlet”的DispatcherServlet的Null ModelAndView:假设HandlerAdapter已完成请求处理 已成功完成请求
我尝试了不同的方法,包括在控制器级别使用@RequestParam,但仍然没有成功。

它抱怨您在
GetMapping
中定义了参数。这很好:

@GetMapping(value = "/customer/v1/user/{userId}/purchase")
public ResponseDto Test(@PathVariable("userId") String userId,@RequestParam("productId") String productId ){
    return  new ResponseDto();
}
它会自动将值放在
productId
变量的url中
productId
之后


如果要添加并非总是必需的
@RequestParam
元素,可以这样定义它们:
@RequestParam(value=“count”,required=false)字符串计数
,请注意
required
部分?如果您根据需要定义它,并且没有在url中传递它,Spring将抛出一个错误。您还可以定义
defaultValue
,这也将解决您的问题。请参阅。

它抱怨您在
GetMapping
中定义了参数。这很有效罚款:

@GetMapping(value = "/customer/v1/user/{userId}/purchase")
public ResponseDto Test(@PathVariable("userId") String userId,@RequestParam("productId") String productId ){
    return  new ResponseDto();
}
它会自动将值放在
productId
变量的url中
productId
之后


如果要添加并非总是必需的
@RequestParam
元素,可以这样定义它们:
@RequestParam(value=“count”,required=false)字符串计数
,请注意
required
部分?如果您根据需要定义它并且没有在url中传递它,Spring将抛出一个错误。您还可以定义
默认值
,这也将解决您的问题。请参阅。

您不需要指定“productId”在@GetMapping的值中。它将被自动拾取。

您不需要指定“productId”在@GetMapping的值中。它将被自动拾取。

谢谢。这很有效。但它不支持多个查询参数。我想了解的是:它怎么不支持它呢?你能不能把
@RequestParam(“count”)放进去
作为函数中的额外参数?恐怕它不起作用,我尝试了,但出现了以下错误:MissingPathVariableException:类型为的方法参数缺少URI模板变量“count”String@Mostafa我已经更新了答案,添加了可选的
@RequestParam
元素的解释。谢谢。这很有效。但是它不起作用支持多个查询参数。我想达到的是这样的:它怎么不支持它?你能不能不把
@RequestParam(“count”)放进去
作为函数中的额外参数?恐怕它不起作用,我尝试了,但出现了以下错误:MissingPathVariableException:类型为的方法参数缺少URI模板变量“count”String@Mostafa我已经更新了答案,包括可选
@RequestParam
元素的解释。