Get the App
SLTechnology News&Howtos  ›  Development  › 

How to make path parameters optional in Spring Rest interface

Shulou Source: shulou.com Published: 2022-06-02 20:10:29 09月21日 Update

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!

Tags: Parameters paths methods traditions that is ways annotations styles interfaces elegance two simultaneously forms articles results errors services bloated not big business Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple OPPO Reno Huawei Shulou Technology MySQL