java如果属性为空返回其他,如果RequestBody参数的某些属性为null,如何返回400 HTTP错误代码?...

I have the following example:

This is the request body:

public class UserLoginData

implements Serializable {

private static final long serialVersionUID = 1L;

private String username;

private String password;

//... getter and setters

}

This is the Controller:

@RequestMapping(value = {"/login"}, method = RequestMethod.POST)

@ResponseBody

public LoginResponse login(@RequestBody(required = true) UserLoginData loginData){

//... some code

}

This is how I invoke the service:

POST /login

{"username":"neuquino"}

I expect that Spring returns a HTTP 400 BAD REQUEST error, because password is missing. But instead of that, it returns a HTTP 500 INTERNAL SERVER error with the following stacktrace:

java.lang.NullPointerException

at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:948) ~[spring-webmvc-3.2.2.RELEASE.jar:3.2.2.RELEASE]

at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:838) ~[spring-webmvc-3.2.2.RELEASE.jar:3.2.2.RELEASE]

at javax.servlet.http.HttpServlet.service(HttpServlet.java:755)

...

How can I specify to Spring that username and password are required fields in request body?

解决方案

@Bart's answer was very useful to find my final solution:

public class UserLoginData

implements Serializable {

private static final long serialVersionUID = 1L;

@NotNull

@NotBlank

private String username;

@NotNull

@NotBlank

private String password;

//... getter and setters

}

On my Controller I have:

public LoginResponse login(

@RequestBody(required = true) @Valid UserLoginData loginData){

//... login code

}

Until here is very similar, but it is clearer because the controller's method does not have the error validation. Instead of that, I used another class with the ControllerAdvice annotation

@ControllerAdvice

public class RestErrorHandler {

private MessageSource messageSource;

@Autowired

public RestErrorHandler(@Qualifier("messageSource") MessageSource messageSource) {

this.messageSource = messageSource;

}

@ExceptionHandler(MethodArgumentNotValidException.class)

@ResponseStatus(HttpStatus.BAD_REQUEST)

@ResponseBody

public ValidationError processValidationError(MethodArgumentNotValidException ex) {

BindingResult result = ex.getBindingResult();

List fieldErrors = result.getFieldErrors();

return this.processFieldErrors(fieldErrors);

}

private ValidationError processFieldErrors(List fieldErrors) {

ValidationError dto = new ValidationError();

for (FieldError fieldError : fieldErrors) {

String localizedErrorMessage = this.resolveLocalizedErrorMessage(fieldError);

dto.getErrors().put(fieldError.getField(), localizedErrorMessage);

}

return dto;

}

private String resolveLocalizedErrorMessage(FieldError fieldError) {

Locale currentLocale = LocaleContextHolder.getLocale();

String localizedErrorMessage = this.messageSource.getMessage(fieldError, currentLocale);

return localizedErrorMessage;

}

}

Now my service is returning this:

{

"errors":{

"country":"country cannot be null"

}

}

I hope it helps someone else.

To get this solution I also used what is written in this post.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值