我写了这个方法来处理类的验证:
@RequestMapping(produces = {"application/vnd.captech-v1.0+json", "application/vnd.captech-v2.0+json"})
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
public @ResponseBody ResultDTO handleValidationException(MethodArgumentNotValidException ex) throws IOException {
ResultDTO result = new ResultDTO();
Map<String, Object> map = Maps.newHashMap();
map.put("error", "Validation Failure");
map.put("violations", convertConstraintViolation(ex));
result.setMap(map);
return result;
}
发生异常时,该方法返回HttpStatus.BAD_REQUEST,但不返回@ResponseBody。 为什么?谢谢!
更新:
这是我的处理班:
package it.cle.project.validation;
import it.cle.project.dto.ResultDTO;
import java.io.IOException;
import java.util.Map;
import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.HttpStatus;
import org.springframework.orm.ObjectRetrievalFailureException;
import org.springframework.validation.ObjectError;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.UnsatisfiedServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import com.google.common.collect.Maps;
@ControllerAdvice
public class DefaultExceptionHandler {
@RequestMapping(produces = {"application/vnd.captech-v1.0+json", "application/vnd.captech-v2.0+json"})
@ExceptionHandler({MissingServletRequestParameterException.class,
UnsatisfiedServletRequestParameterException.class,
HttpRequestMethodNotSupportedException.class,
ServletRequestBindingException.class
})
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
public @ResponseBody ResultDTO handleRequestException(Exception ex) {
Map<String, Object> map = Maps.newHashMap();
map.put("error", "Request Error");
map.put("cause", ex.getMessage());
ResultDTO result = new ResultDTO();
result.setMap(map);
return result;
}
@RequestMapping(produces = {"application/vnd.captech-v1.0+json", "application/vnd.captech-v2.0+json"})
@ExceptionHandler(ConstraintViolationException.class)
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
public @ResponseBody ResultDTO handleValidationException(ConstraintViolationException ex) throws IOException {
Map<String, Object> map = Maps.newHashMap();
map.put("error", "Validation Failure");
map.put("violations", convertConstraintViolation(ex.getConstraintViolations()));
ResultDTO result = new ResultDTO();
result.setMap(map);
return result;
}
@RequestMapping(produces = {"application/vnd.captech-v1.0+json", "application/vnd.captech-v2.0+json"})
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
public @ResponseBody ResultDTO handleValidationException(MethodArgumentNotValidException ex) throws IOException {
Map<String, Object> map = Maps.newHashMap();
map.put("error", "Validation Failure");
map.put("violations", convertConstraintViolation(ex));
ResultDTO result = new ResultDTO();
result.setMap(map);
return result;
}
@RequestMapping(produces = {"application/vnd.captech-v1.0+json", "application/vnd.captech-v2.0+json"})
@ExceptionHandler(ObjectRetrievalFailureException.class)
@ResponseStatus(value = HttpStatus.NOT_FOUND)
public @ResponseBody ResultDTO handleValidationException(ObjectRetrievalFailureException ex) throws IOException {
Map<String, Object> map = Maps.newHashMap();
map.put("error", "Entity Not Found");
map.put("cause", ex.getMessage());
ResultDTO result = new ResultDTO();
result.setMap(map);
return result;
}
@RequestMapping(produces = {"application/vnd.captech-v1.0+json", "application/vnd.captech-v2.0+json"})
@ExceptionHandler(DataIntegrityViolationException.class)
@ResponseStatus(value = HttpStatus.CONFLICT)
public @ResponseBody ResultDTO handleDataIntegrityViolationException(DataIntegrityViolationException ex) throws IOException {
Map<String, Object> map = Maps.newHashMap();
map.put("error", "Data Integrity Error");
map.put("cause", ex.getCause().getCause().getLocalizedMessage());
ResultDTO result = new ResultDTO();
result.setMap(map);
return result;
}
@RequestMapping(produces = {"application/vnd.captech-v1.0+json", "application/vnd.captech-v2.0+json"})
@ExceptionHandler(DataAccessException.class)
@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
public @ResponseBody ResultDTO handleDataAccessException(DataAccessException ex) throws IOException {
Map<String, Object> map = Maps.newHashMap();
map.put("error", "Data Error");
map.put("cause", ex.getCause().getMessage());
ResultDTO result = new ResultDTO();
result.setMap(map);
return result;
}
@RequestMapping(produces = {"application/vnd.captech-v1.0+json", "application/vnd.captech-v2.0+json"})
@ExceptionHandler(HttpMediaTypeNotSupportedException.class)
@ResponseStatus(value = HttpStatus.UNSUPPORTED_MEDIA_TYPE)
public @ResponseBody ResultDTO handleUnsupportedMediaTypeException(HttpMediaTypeNotSupportedException ex) throws IOException {
Map<String, Object> map = Maps.newHashMap();
map.put("error", "Unsupported Media Type");
map.put("cause", ex.getLocalizedMessage());
map.put("supported", ex.getSupportedMediaTypes());
ResultDTO result = new ResultDTO();
result.setMap(map);
return result;
}
@RequestMapping(produces = {"application/vnd.captech-v1.0+json", "application/vnd.captech-v2.0+json"})
@ExceptionHandler(Exception.class)
@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
public @ResponseBody ResultDTO handleUncaughtException(Exception ex) throws IOException {
Map<String, Object> map = Maps.newHashMap();
map.put("error", "Unknown Error");
if (ex.getCause() != null) {
map.put("cause", ex.getCause().getMessage());
} else {
map.put("cause", ex.getMessage());
}
ResultDTO result = new ResultDTO();
result.setMap(map);
return result;
}
private Map<String, Map<String, Object> > convertConstraintViolation(Set<ConstraintViolation<?>> constraintViolations) {
Map<String, Map<String, Object> > result = Maps.newHashMap();
for (ConstraintViolation constraintViolation : constraintViolations) {
Map<String, Object> violationMap = Maps.newHashMap();
violationMap.put("value", constraintViolation.getInvalidValue());
violationMap.put("type", constraintViolation.getRootBeanClass());
violationMap.put("message", constraintViolation.getMessage());
result.put(constraintViolation.getPropertyPath().toString(), violationMap);
}
return result;
}
private Map<String, Map<String, Object> > convertConstraintViolation(MethodArgumentNotValidException ex) {
Map<String, Map<String, Object> > result = Maps.newHashMap();
for (ObjectError error : ex.getBindingResult().getAllErrors()) {
Map<String, Object> violationMap = Maps.newHashMap();
violationMap.put("target", ex.getBindingResult().getTarget());
violationMap.put("type", ex.getBindingResult().getTarget().getClass());
violationMap.put("message", error.getDefaultMessage());
result.put(error.getObjectName(), violationMap);
}
return result;
}
}
当我调用(使用错误的params)my方法时,会调用handleValidationException(ConstraintViolationException ex)方法。 我的方法验证IndiceBarthel实体并抛出异常。
@RequestMapping(value = "/indiceBarthel", method = RequestMethod.POST)
public ResultDTO createIndiceBarthel(@Valid @RequestBody IndiceBarthel indiceBarthel) throws Exception {
ResultDTO result = new ResultDTO();
Map<String, Object> map = Maps.newHashMap();
try {
ApplicationContext context = new ClassPathXmlApplicationContext("context.xml");
IndiceBarthelService indiceBarthelService = (IndiceBarthelService) context.getBean("indiceBarthelService");
indiceBarthelService.createIndiceBarthel(indiceBarthel);
} catch (Exception e) {
throw new Exception();
}
map.put("result", "Inserimento effettuato");
map.put("entity", indiceBarthel);
result.setMap(map);
return result;
}
更新: 我不知道为什么,但问题取决于方法调用:如果我从RESTClient调用(RESTful Web服务的调试器)是好的,否则如果我从RestTemplate调用返回404错误,崩溃并且不返回JSON错误。 ..
答案 0 :(得分:0)
您确定控制器的方法既可以是映射也可以是异常处理程序?如果可以(我之前从未检查或看过),@ResponseStatus适用于哪两个案例?这似乎是一种不应存在的歧义
我强烈建议你为异常处理制作一个单独的方法