Return 错误列表
Return list of errors
我想 return 使用此 Response 对象的错误列表:
public class StringResponseDTO {
private String response;
public StringResponseDTO(String response) {
super();
this.response = response;
}
public String getResponse() {
return response;
}
public void setResponse(String response) {
this.response = response;
}
}
我使用这段代码产生错误:
List<FieldError> errors = result.getFieldErrors();
for (FieldError error : errors ) {
System.out.println ("Validation error in field: " + error.getObjectName()
+ "! Validation error message: " + error.getDefaultMessage()
+ "! Rejected value:" + error.getRejectedValue());
return ResponseEntity.ok(new StringResponseDTO(error.getField() + " " + error.getDefaultMessage()));
}
我想要return这样的列表:
response: {
errors: [
field_name: message,
second_name: second_message
]
}
你知道我怎么修改代码吗?可能我需要添加构造函数?
response: {
errors: [
field_name: message,
second_name: second_message
]
}
你需要使用下面的类来模拟上面的json:
@JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT, use = JsonTypeInfo.Id.NAME)
@JsonTypeName("response")
class StringResponseDTO {
private List<String> errors;
public StringResponseDTO(final List<String> errors) {
this.errors = errors;
}
public List<String> getErrors() {
return errors;
}
public void setErrors(final List<String> errors) {
this.errors = errors;
}
}
您可以将响应构建为:
List<String> errorsList = new ArrayList<>();
List<FieldError> errors = result.getFieldErrors();
for (FieldError error : errors ) {
System.out.println ("Validation error in field: " + error.getObjectName()
+ "! Validation error message: " + error.getDefaultMessage()
+ "! Rejected value:" + error.getRejectedValue());
errorsList.add(error.getField() + " " + error.getDefaultMessage());
}
return ResponseEntity.badRequest().body(new StringResponseDTO(errorsList));
我想 return 使用此 Response 对象的错误列表:
public class StringResponseDTO {
private String response;
public StringResponseDTO(String response) {
super();
this.response = response;
}
public String getResponse() {
return response;
}
public void setResponse(String response) {
this.response = response;
}
}
我使用这段代码产生错误:
List<FieldError> errors = result.getFieldErrors();
for (FieldError error : errors ) {
System.out.println ("Validation error in field: " + error.getObjectName()
+ "! Validation error message: " + error.getDefaultMessage()
+ "! Rejected value:" + error.getRejectedValue());
return ResponseEntity.ok(new StringResponseDTO(error.getField() + " " + error.getDefaultMessage()));
}
我想要return这样的列表:
response: {
errors: [
field_name: message,
second_name: second_message
]
}
你知道我怎么修改代码吗?可能我需要添加构造函数?
response: {
errors: [
field_name: message,
second_name: second_message
]
}
你需要使用下面的类来模拟上面的json:
@JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT, use = JsonTypeInfo.Id.NAME)
@JsonTypeName("response")
class StringResponseDTO {
private List<String> errors;
public StringResponseDTO(final List<String> errors) {
this.errors = errors;
}
public List<String> getErrors() {
return errors;
}
public void setErrors(final List<String> errors) {
this.errors = errors;
}
}
您可以将响应构建为:
List<String> errorsList = new ArrayList<>();
List<FieldError> errors = result.getFieldErrors();
for (FieldError error : errors ) {
System.out.println ("Validation error in field: " + error.getObjectName()
+ "! Validation error message: " + error.getDefaultMessage()
+ "! Rejected value:" + error.getRejectedValue());
errorsList.add(error.getField() + " " + error.getDefaultMessage());
}
return ResponseEntity.badRequest().body(new StringResponseDTO(errorsList));