简体   繁体   中英

How to use @Pathvariable and @RequestParam at the same time in Spring Rest

I'm trying to provide a rest service in spring-boot in order to match requests like this :

http://localhost:8080/customer/v1/user/1/purchase?productId=2

So, I've created a service like this :

@GetMapping(value = "/customer/v1/user/{userId}/purchase?productId={productId}")
public ResponseDto Test(@PathVariable("userId") String userId,@RequestParam("productId") String productId ){
    return  new ResponseDto();
}

But it seems it does not match the request and I get this error :

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

I've tried different way including using @RequestParam at Controller-Level, But still no success.

It's complaining about you defining the parameters in your GetMapping . This works fine:

@GetMapping(value = "/customer/v1/user/{userId}/purchase")
public ResponseDto Test(@PathVariable("userId") String userId,@RequestParam("productId") String productId ){
    return  new ResponseDto();
}

It will automatically put the value after productId in the url in the productId variable.

If you wanted to add @RequestParam elements which are not always required you can define them like this: @RequestParam(value = "count", required=false) String count , notice the required part? Spring will throw an error if you define it as required and you're not passing it in the url. You could also define the defaultValue which will solve your issue aswell. See Spring Docs .

You don't need to specify the "productId" in the value of @GetMapping. It will be automatically picked up.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM