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 automatically populate the instance object with @ PathVariable of SpringBoot

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "how the @ PathVariable of SpringBoot is automatically filled into the instance object". The explanation in the article is simple and clear, and is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to automatically fill the @ PathVariable of SpringBoot into the instance object".

Catalogue

@ PathVariable automatically populates the instance object

Remember the problems encountered in the use of SpringBoot @ PathVariable

The first question.

Solution.

The second question

Solution.

@ PathVariable automatically populates the instance object

Just record a @ PathVariable tip just used today so as not to forget it later.

@ PostMapping ("/ updateSeeker/ {userid}") @ ResponseBodypublic String updateSeeker (@ PathVariable ("userid") Integer id,Seeker seeker) {System.out.println ("the incoming seeker is =" + seeker) / / the returned value @ PathVariable ("userid") will automatically populate the returned value if there is a duplicate attribute in the seeker object / / for example, because there is a userid attribute in the seeker object, the returned userid will automatically populate the scenic spot return "wdnmd"} the first problem encountered in the use of SpringBoot @ 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:

Thank you for your reading. the above is the content of "how SpringBoot @ PathVariable is automatically populated into instance objects". After the study of this article, I believe you have a deeper understanding of how SpringBoot @ PathVariable is automatically populated into instance objects, and the specific usage needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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