- Mastering Microservices with Java 9(Second Edition)
- Sourabh Sharma
- 529字
- 2021-07-02 21:54:45
The @PathVariable annotation
@PathVariable helps you to create the dynamic URIs. @PathVariable annotation allows you to map Java parameters to a path parameter. It works with @RequestMapping where the placeholder is created in URI 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 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, that 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:
CalculationController also uses the regular expression .+ in the path variable (path parameter) to allow the decimal point(.) in numeric values: /path/{variable:.+}. Spring ignores anything after the last dot. Spring default behavior takes it as a file extension.
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+).
Code of CalculationController resource, where we have implemented to 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 URI /calculation/power and /calculation/sqrt.
Here, we are using sqrt and power as a part of the URI, which we have used for demonstration purposes only. Ideally, these should have been passed as the value of a request parameter function, or something similar based on endpoint design formation.
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.
- Qt 5 and OpenCV 4 Computer Vision Projects
- 軟件架構設計:大型網站技術架構與業務架構融合之道
- Android 9 Development Cookbook(Third Edition)
- JavaScript前端開發與實例教程(微課視頻版)
- SEO智慧
- Full-Stack Vue.js 2 and Laravel 5
- 用戶體驗增長:數字化·智能化·綠色化
- Learning Laravel's Eloquent
- Scala Functional Programming Patterns
- NGUI for Unity
- WordPress Search Engine Optimization(Second Edition)
- Python數據科學實踐指南
- Offer來了:Java面試核心知識點精講(框架篇)
- Java程序設計及應用開發
- Flutter從0基礎到App上線