- Mastering Microservices with Java
- Sourabh Sharma
- 538字
- 2021-07-02 13:03:32
The @PathVariable annotation
@PathVariable helps you to create the dynamic URIs. The @PathVariable annotation allows you to map Java parameters to a path parameter. It works with @RequestMapping, where the placeholder is created in a URI, and then the same placeholder name is used either as a PathVariable or a method parameter, as you can see in the CalculationController class method sqrt(). Here, the value placeholder is created inside the @RequestMapping annotation and the same value is assigned to the value of the @PathVariable.
The sqrt() method takes the parameter in the URI in place of the request parameter, for example, http://localhost:8080/calculation/sqrt/144. Here, the 144 value is passed as the path parameter and this URL should return the square root of 144, which is 12.
To use the basic check in place, we use the regular expression, "^-?+\\d+\\.?+\\d*$", to allow only valid numbers in parameters.
If non-numeric values are passed, the respective method adds an error message to the output key of the JSON:
There are other alternatives, such as adding a slash at the end (/path/{variable}/), or overriding the configurePathMatch()method of WebMvcConfigurerAdapter by setting the useRegisteredSuffixPatternMatch to true, using PathMatchConfigurer (available in Spring 4.0.1+).
The following is the code of the CalculationController resource, where we have implemented two REST endpoints:
package com.packtpub.mmj.rest.resources; import com.packtpub.mmj.lib.model.Calculation; import java.util.ArrayList; import java.util.List; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import static org.springframework.web.bind.annotation.RequestMethod.GET; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; /** * * @author sousharm */ @RestController @RequestMapping("calculation") public class CalculationController { private static final String PATTERN = "^-?+\\d+\\.?+\\d*$"; /** * * @param b * @param e * @return */ @RequestMapping("/power") public Calculation pow(@RequestParam(value = "base") String b, @RequestParam(value = "exponent") String e) { List<String> input = new ArrayList(); input.add(b); input.add(e); List<String> output = new ArrayList(); String powValue; if (b != null && e != null && b.matches(PATTERN) && e.matches(PATTERN)) { powValue = String.valueOf(Math.pow(Double.valueOf(b), Double.valueOf(e))); } else { powValue = "Base or/and Exponent is/are not set to numeric value."; } output.add(powValue); return new Calculation(input, output, "power"); } /** * * @param aValue * @return */ @RequestMapping(value = "/sqrt/{value:.+}", method = GET) public Calculation sqrt(@PathVariable(value = "value") String aValue) { List<String> input = new ArrayList<>(); input.add(aValue); List<String> output = new ArrayList<>(); String sqrtValue; if (aValue != null && aValue.matches(PATTERN)) { sqrtValue = String.valueOf(Math.sqrt(Double.valueOf(aValue))); } else { sqrtValue = "Input value is not set to numeric value."; } output.add(sqrtValue); return new Calculation(input, output, "sqrt"); } }
Here, we are exposing only the power and sqrt functions for the Calculation resource using the /calculation/power and /calculation/sqrt URIs.
One interesting thing here is that due to Spring's HTTP message converter support, the Calculation object gets converted to JSON automatically. You don't need to do this conversion manually. If Jackson 2 is on the classpath, Spring's MappingJackson2HttpMessageConverter converts the Calculation object to JSON.
- 一步一步學Spring Boot 2:微服務項目實戰
- VMware View Security Essentials
- 解構產品經理:互聯網產品策劃入門寶典
- 碼上行動:零基礎學會Python編程(ChatGPT版)
- 名師講壇:Java微服務架構實戰(SpringBoot+SpringCloud+Docker+RabbitMQ)
- 人人都懂設計模式:從生活中領悟設計模式(Python實現)
- Teaching with Google Classroom
- Python 3.7從入門到精通(視頻教學版)
- 計算機應用基礎項目化教程
- Hack與HHVM權威指南
- Python機器學習開發實戰
- Android開發權威指南(第二版)
- Elasticsearch實戰(第2版)
- Continuous Integration,Delivery,and Deployment
- jQuery權威指南