Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problems encountered in using @ PathVariable in SpringBoot

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how to solve the problems encountered in the use of @ PathVariable in SpringBoot, I believe most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's learn about it!

The first problem encountered when using @ PathVariable

Interface:

/ * query customer details * BY Oliver_Deng * @ param userId user id * @ return * / @ ApiOperation (value= "query customer details", notes = "query customer details based on incoming id") @ ApiResponses ({@ ApiResponse (request parameters not filled in ")}) @ ApiImplicitParam (paramType =" query ", name =" userId ", dataType =" Integer ", required = true Value = "customer id") @ GetMapping ("/ findCustomerDetailByUserId/ {userId}") public Result findCustomerByUserId (@ PathVariable Integer userId) {/ / customer Information / / CustomerListVo customerMessage = customerUserService.findCustomerByUserID (userId) / / customer vehicle information / / List cars = customerUserCarService.findCarByUserId (Integer.valueOf (userId)); / / not finish yet / / TODO count card, stored value card return null;}

Swagger access:

Solution.

Just set paramType to path.

ParamType: indicates where to put the parameter

Header-- > request parameter acquisition: @ RequestHeader (receive comments in the code)

Query-- > request parameter acquisition: @ RequestParam (receive comments in the code)

Path (for restful interface)-- > request parameter acquisition: @ PathVariable (receive comments in code)

Body-- > request parameter acquisition: @ RequestBody (receive comments in the code)

Form (not commonly used)

The second question

Interface:

/ * query vehicle brand, car series, annual model, vehicle model information * BY Oliver_Deng * @ param brandId vehicle brand ID * @ param familyId vehicle series ID * @ param groupId vehicle annual model ID * @ return * / @ ApiOperation (value= "query vehicle brand, car series, annual model, vehicle model information", notes = "query vehicle brand, car series, annual model, vehicle model information. Clear the superior id before entering the sub-id." You can enter only one id or no id per visit) @ ApiImplicitParams ({@ ApiImplicitParam (paramType = "query", name = "brandId", dataType = "String", required = false, value = "vehicle brand ID"), @ ApiImplicitParam (paramType = "query", name = "familyId", dataType = "String", required = false, value = "car ID"), @ ApiImplicitParam (paramType = "query", name = "groupId", dataType = "String", required = false) Value = "vehicle ID") @ ApiResponses ({@ ApiResponse (code=400,message= "request parameters not filled") @ GetMapping ("/ findCarBrandMessage/ {brandId} / {familyId} / {groupId}") public Result findCarBrandMessage (@ PathVariable String brandId,@PathVariable String familyId,@PathVariable String groupId) {return customerUserCarService.findCarBrandMessage (brandId,familyId,groupId) }

Swageer access:

The query was successful, but there is no data. Let's take a look at DeBug:

You can see that when the value is actually these, it is not null, so the if condition is naturally satisfied, but the function is not like this.

Solution.

Remove the @ ApiImplicitParam parameter and use @ RequestParam ().

Why can I use @ RequestParam ()?

RequestParam is the parameter concatenated after url.

Success:

@ PathVariable 404 question

Jump page:

Controller layer:

After that, console did not report an error, but

Change the method:

After correction:

The above is all the contents of this article entitled "how to solve the problems encountered in the use of @ PathVariable in SpringBoot". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report