注释 Web 方法参数时 JAX-RS Jersey 客户端获得 400 响应
JAX-RS Jersey client gets 400 response when web method parameters are annotated
这是我的网络服务方法:
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@POST
@Path("login")
public Response login(@NotNull @Valid Credentials credentials) {
// do login
}
这是客户端代码片段:
WebTarget loginTarget = baseTarget
.path("base")
.path("login");
Credentials credentials = new Credentials(username, password);
Response resp = loginOperation
.request()
.post(
Entity.entity(credentials, MediaType.APPLICATION_JSON_TYPE)
);
当我post时,它没有到达登录方法。服务器 returns 400 错误,正文为空。
当我从 credentials 参数中删除 @NotNull @Valid
注释时,它起作用了。
我注意到 Entity#entity 方法有一个重载版本,它接受 Annotation[]
作为第三个参数。然后我看到了 this section 的 Jersey 文档。所以我按照教程中的建议继续创建注释工厂:
public static class ValidFactory extends AnnotationLiteral<Valid> implements Valid {
public static ValidFactory get() {
return new ValidFactory();
}
}
然后将客户端代码更改为:
.post(
Entity.entity(credentials, MediaType.APPLICATION_JSON_TYPE,
new Annotation[] {
AuthenticationResource.NotNullFactory.get(),
AuthenticationResource.ValidFactory.get()
}
)
)
不幸的是,这导致了同样的错误。
Google 搜索没有得到任何结果,我也没有太多时间去研究 Jersey 源代码。那么,也许知道解决方案的人可以分享一下,好吗?
更新
只是添加到@peeskillet 的回复中。我使用自定义 ExceptionMapper:
@Provider
public class ValidationExceptionMapper implements ExceptionMapper<ConstraintViolationException> {
@Override
public Response toResponse(ConstraintViolationException exception) {
// customize response
}
}
所以,在我的例子中,我不得不在服务器配置中注册映射器,而不是设置 ServerProperties.BV_SEND_ERROR_IN_RESPONSE
属性:
GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI)
new ResourceConfig() {
{
register(ValidationExceptionMapper.class)
}
}
);
听起来你只需要 configure Jersey to send error messages using ServerProperties.BV_SEND_ERROR_IN_RESPONSE:
public static final String BV_SEND_ERROR_IN_RESPONSE
A Bean Validation (JSR-349) support customization property. If set to true and Bean Validation support has not been explicitly disabled (see BV_FEATURE_DISABLE), the validation error information will be sent in the entity of the returned Response.
The default value is false. This means that in case of an error response caused by a Bean Validation error, only a status code is sent in the server Response by default.
The name of the configuration property is "jersey.config.beanValidation.enableOutputValidationErrorEntity.server".
在你的ResourceConfig
property(ServerProperties.BV_SEND_ERROR_IN_RESPONSE, true);
或在您的 web.xml
<init-param>
<param-name>
jersey.config.beanValidation.enableOutputValidationErrorEntity.server
</param-name>
<param-value>true</param-value>
</init-param>
这是我的网络服务方法:
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@POST
@Path("login")
public Response login(@NotNull @Valid Credentials credentials) {
// do login
}
这是客户端代码片段:
WebTarget loginTarget = baseTarget
.path("base")
.path("login");
Credentials credentials = new Credentials(username, password);
Response resp = loginOperation
.request()
.post(
Entity.entity(credentials, MediaType.APPLICATION_JSON_TYPE)
);
当我post时,它没有到达登录方法。服务器 returns 400 错误,正文为空。
当我从 credentials 参数中删除 @NotNull @Valid
注释时,它起作用了。
我注意到 Entity#entity 方法有一个重载版本,它接受 Annotation[]
作为第三个参数。然后我看到了 this section 的 Jersey 文档。所以我按照教程中的建议继续创建注释工厂:
public static class ValidFactory extends AnnotationLiteral<Valid> implements Valid {
public static ValidFactory get() {
return new ValidFactory();
}
}
然后将客户端代码更改为:
.post(
Entity.entity(credentials, MediaType.APPLICATION_JSON_TYPE,
new Annotation[] {
AuthenticationResource.NotNullFactory.get(),
AuthenticationResource.ValidFactory.get()
}
)
)
不幸的是,这导致了同样的错误。 Google 搜索没有得到任何结果,我也没有太多时间去研究 Jersey 源代码。那么,也许知道解决方案的人可以分享一下,好吗?
更新
只是添加到@peeskillet 的回复中。我使用自定义 ExceptionMapper:
@Provider
public class ValidationExceptionMapper implements ExceptionMapper<ConstraintViolationException> {
@Override
public Response toResponse(ConstraintViolationException exception) {
// customize response
}
}
所以,在我的例子中,我不得不在服务器配置中注册映射器,而不是设置 ServerProperties.BV_SEND_ERROR_IN_RESPONSE
属性:
GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI)
new ResourceConfig() {
{
register(ValidationExceptionMapper.class)
}
}
);
听起来你只需要 configure Jersey to send error messages using ServerProperties.BV_SEND_ERROR_IN_RESPONSE:
public static final String BV_SEND_ERROR_IN_RESPONSE
A Bean Validation (JSR-349) support customization property. If set to true and Bean Validation support has not been explicitly disabled (see BV_FEATURE_DISABLE), the validation error information will be sent in the entity of the returned Response.
The default value is false. This means that in case of an error response caused by a Bean Validation error, only a status code is sent in the server Response by default.
The name of the configuration property is "jersey.config.beanValidation.enableOutputValidationErrorEntity.server".
在你的ResourceConfig
property(ServerProperties.BV_SEND_ERROR_IN_RESPONSE, true);
或在您的 web.xml
<init-param>
<param-name>
jersey.config.beanValidation.enableOutputValidationErrorEntity.server
</param-name>
<param-value>true</param-value>
</init-param>