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 make path parameters optional in Spring Rest interface

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

Share

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

Editor to share with you how to make the path parameters in the Spring Rest interface optional. I hope you will get something after reading this article. Let's discuss it together.

Spring Rest interface path parameters are optional

I have a Spring Rest service where one path parameter is optional (the reality is that I originally put the parameter in the path, while the other front end is passed to me through body). The traditional way is to divide the service into two methods in the code, one with path parameters and the other without, but this looks inelegant and confusing.

I tried to annotate @ PathVariable with require=false, but it didn't work and returned a 404 error.

The following form is the traditional way:

@ RequestMapping (value = "/ path/ {id}") public String getResource (@ PathVariable String id) {return service.processResource (id);} @ RequestMapping (value = "/ path") public String getResource () {return service.processResource ();}

But I really don't like this way, bloated.

Starting with Spring 4 and Java 8 (which actually has little to do with Java 8), it becomes easy to implement optional path parameters in one method, as shown below, to define two routes at the same time:

RequestMapping (value = {"/ path", "/ path/ {id}") public String getResource (@ PathVariable Optional id) {if (id.isPresent ()) {return service.processResource (id.get ());} else {return service.processResource ();}}

Indeed, it is much more elegant to unify business in one way.

Pass parameters in RestFul style

The RestFul style is that all parameters are passed by / instead of the traditional xx&xx form

For example: write a Controller:

Package controller;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class RestfulController {@ RequestMapping ("/ add") public String test (int a _ reint b, Model model) {int res = axiombscape model.addAttribute ("msg", "result is" + res); return "test";}}

You can see that there is an error that ABI b did not find.

Pass parameters in the traditional way:? a=1&b=2

Then you should pass parameters according to Restful style: add @ PathVariable annotation to the method parameter value, and bind the parameter directly on url. You can set the method type of Request (GET, POST, DELETE, OPTIONS, HEAD, PATCH, PUT, TRACE) at the same time.

@ PathVariable: bind the value of the method parameter to a url template variable

Package controller;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class RestfulController {@ RequestMapping (value = "/ add/ {a} / {b}", method = RequestMethod.GET) public String test (@ PathVariable int an int b, Model model) {int res = "msg", "result is" + res "; return" test ";}}

Turn on Tomcat again, and set axi1djimg 3:

/ add/1/3 pass parameters

This is the restful style parameter transfer.

It can also be achieved through disguised combination annotations:

@ PostMapping

@ GetMapping

@ PutMapping

@ DeleteMapping

@ PatchMapping

After reading this article, I believe you have a certain understanding of "how to make the path parameters optional in the Spring Rest interface". If you want to know more about it, welcome to follow the industry information channel. Thank you for reading!

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

  • How to use css to realize the Animation effect of Water pattern Diffusion

    Editor to share with you how to use css to achieve watermark diffusion animation effect, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

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

    12
    Report