Spring验证相关请求参数
在这种情况下,如果纬度不为空,则经度必须不为空 如何验证相关的请求参数?Spring验证相关请求参数,spring,spring-mvc,spring-boot,Spring,Spring Mvc,Spring Boot,在这种情况下,如果纬度不为空,则经度必须不为空 如何验证相关的请求参数? 是否有现成的解决方案,或手动验证?如果您使用的是最新版本的spring,则有一个注释@Validated 使用此注释,我们可以验证@RequestParam和@PathVariable 需要注意的最重要的一点是,用于验证请求主体的@Valid抛出异常MethodArgumentNotValidException 而@Validated抛出ConstraintViolationException 由于这两种异常的类型不同,您
是否有现成的解决方案,或手动验证?如果您使用的是最新版本的spring,则有一个注释
@Validated
使用此注释,我们可以验证@RequestParam
和@PathVariable
需要注意的最重要的一点是,用于验证请求主体的@Valid
抛出异常MethodArgumentNotValidException
而@Validated
抛出ConstraintViolationException
由于这两种异常的类型不同,您必须使用@ExceptionHandler
以不同的方式处理它们,如下所示:-
@RequestMapping(value = "/test")
@ResponseBody
public Object test(@RequestParam("latitude") String latitude,
@RequestParam("longitude") String longitude) {
}
@ExceptionHandler(值={ConstraintViolationException.class})
@ResponseStatus(值=HttpStatus.BAD_请求)
公共责任处理约束约束约束例外(约束约束例外e){
//您的代码将在此处相应地处理异常
}
@ExceptionHandler(值={MethodArgumentNotValidException.class})
@ResponseStatus(值=HttpStatus.BAD_请求)
公共响应处理方法无效异常(方法论无效异常e){
//您的代码将在此处相应地处理异常
}
以下是一种方法:
@RequestParam
sLongLatException
下面创建一个自定义异常,并将其直接绑定到您喜欢的HTTP错误代码(在HttpStatus.BAD_REQUEST
下面)@ExceptionHandler(value = { ConstraintViolationException.class })
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
public ResponseEntity<Type> handleConstraintViolationException(ConstraintViolationException e) {
// your code here to handle the exceptions accordingly
}
@ExceptionHandler(value = { MethodArgumentNotValidException.class })
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
public ResponseEntity<Type> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
// your code here to handle the exceptions accordingly
}
您不应该使用字符串来表示纬度或经度。使用适当的数据类型:double或BigDecimal。double不可能为null,Spring将对此进行验证。您可以使用Bean验证注释对参数进行注释(
@NotNull
此处)。检查文档。
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyController {
@RequestMapping("/test")
@ResponseBody
public String test(LongLat longLat) {
if(isNotValid(longLat)) {
throw new LongLatException();
}
return "latitude: "+longLat.getLatitude() + ", longtitude: " + longLat.getLongitude();
}
private boolean isNotValid(LongLat longLat) {
return (longLat.getLongitude() == null && longLat.getLatitude() != null) ||
(longLat.getLongitude() == null && longLat.getLatitude() == null);
}
@ResponseStatus(HttpStatus.BAD_REQUEST)
static class LongLatException extends RuntimeException {
public LongLatException() {
super("Longitude and Latitude params either need to be both present or empty");
}
}
static class LongLat {
private final String latitude;
private final String longitude;
public LongLat(String latitude, String longitude) {
this.latitude = latitude;
this.longitude = longitude;
}
public String getLatitude() {
return latitude;
}
public String getLongitude() {
return longitude;
}
@Override
public String toString() {
String sb = "LongLat{" + "latitude='" + latitude + '\'' +
", longitude='" + longitude + '\'' +
'}';
return sb;
}
}
}