gpt4 book ai didi

spring - Jersey 自定义 ExceptionMapper 未在 400 Bad Request 上调用(验证失败时)

转载 作者:行者123 更新时间:2023-12-01 00:45:12 26 4
gpt4 key购买 nike

我正在将 Jersey 与 Spring 一起用于 Web 服务。为了捕获异常并格式化发送给调用者的响应,我添加了一个 ExceptionMapper 的实现。

虽然当我从 Controller 内显式抛出异常时它被调用,但是当 json 字段验证失败时,异常映射器不会被调用并且发送的响应是 ** 可能不是 null (path = checkNotification.arg0.tyres, invalidValue = 空)
**

@Provider
public class GenericExceptionMapper implements ExceptionMapper<Throwable> {

@Override
public Response toResponse(Throwable ex) {
System.out.println("Exception Mapper !!!");
return Response.status(404).entity(ex.getMessage()).type("application/json").build();
}
}


<servlet>
<servlet-name>jersey-servlet</servlet-name>
<servlet-class>
org.glassfish.jersey.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>
com.help.rest.controller,
com.fasterxml.jackson.jaxrs.json
</param-value>
</init-param>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.help.filter.FeatureRegistration</param-value>
</init-param>
<init-param>
<param-name>jersey.config.beanValidation.enableOutputValidationErrorEntity.server</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

Jersey 版本是 2.22.1
Spring 版本是 4.2.4

我通过改变让它工作
 @Provider
public class GenericExceptionMapper implements ExceptionMapper<Throwable> {

@Override
public Response toResponse(Throwable ex) {
System.out.println("Exception Mapper !!!");
return Response.status(404).entity(ex.getMessage()).type("application/json").build();
}
}


 @Provider
public class GenericExceptionMapper implements ExceptionMapper<ConstraintViolationException> {

@Override
public Response toResponse(ConstraintViolationException ex) {
System.out.println("Exception Mapper !!!");
return Response.status(404).entity(ex.getMessage()).type("application/json").build();
}
}

虽然我能够捕捉到异常,但我没有得到约束失败的确切类和字段。

已解决
在 ContraintViolationException 中找到包含所有必填字段的 Set,可以使用 ex.getConstraintViolations() 访问

最佳答案

通过改变使它工作

 @Provider
public class GenericExceptionMapper implements ExceptionMapper<Throwable> {

@Override
public Response toResponse(Throwable ex) {
System.out.println("Exception Mapper !!!");
return Response.status(404).entity(ex.getMessage()).type("application/json").build();
}
}


 @Provider
public class GenericExceptionMapper implements ExceptionMapper<ConstraintViolationException> {

@Override
public Response toResponse(ConstraintViolationException ex) {
System.out.println("Exception Mapper !!!");
return Response.status(404).entity(ex.getMessage()).type("application/json").build();
}
}

要获取确切的异常详细信息,例如哪个字段未通过约束,请使用 ConstraintViolationException 的 getConstraintViolations()。该方法为所有约束违规提供了一个集合。

关于spring - Jersey 自定义 ExceptionMapper 未在 400 Bad Request 上调用(验证失败时),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35065029/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com