ExceptionMapper 实体似乎被 Quarkus 包裹
ExceptionMapper entity seems to get wrapped by Quarkus
在 Quarkus 中,异常映射器返回的实体似乎被包装在另一个实体中。
提供一个 JAX-RS 异常映射器,例如:
@Provider
public class WebhookExceptionMapper implements ExceptionMapper<WebhookException> {
@Override
public Response toResponse(final WebhookException e) {
return Response.status(e.getError().getCode().getStatus())
.entity(Entity.entity(e.getError(), MediaType.APPLICATION_JSON))
.build();
}
}
我收到以下错误响应:
{
"entity": {
"code": "SOME_ERROR_CODE",
"msg": "Error message"
},
"variant": {
"language": null,
"mediaType": {
"type": "application",
"subtype": "json",
"parameters": {},
"wildcardType": false,
"wildcardSubtype": false
},
"encoding": null,
"languageString": null
},
"annotations": [],
"mediaType": {
"type": "application",
"subtype": "json",
"parameters": {},
"wildcardType": false,
"wildcardSubtype": false
},
"language": null,
"encoding": null
}
我希望返回以下内容:
{
"code": "SOME_ERROR_CODE",
"msg": "Error message"
}
这可能吗?
从包名可以看出,javax.ws.rs.client.Entity class 仅用于客户端。在服务器端,您不需要使用它。您实际看到的是正在序列化的 Entity
对象,而不是错误。
如果要设置类型,只需在 Response.ResponseBuilder
上使用 type()
方法(调用 Response.status()
返回)。要设置正文,只需使用 entity()
方法。
return Response.status(e.getError().getCode().getStatus())
.entity(e.getError())
.type(MediaType.APPLICATION_JSON)
.build();
在 Quarkus 中,异常映射器返回的实体似乎被包装在另一个实体中。
提供一个 JAX-RS 异常映射器,例如:
@Provider
public class WebhookExceptionMapper implements ExceptionMapper<WebhookException> {
@Override
public Response toResponse(final WebhookException e) {
return Response.status(e.getError().getCode().getStatus())
.entity(Entity.entity(e.getError(), MediaType.APPLICATION_JSON))
.build();
}
}
我收到以下错误响应:
{
"entity": {
"code": "SOME_ERROR_CODE",
"msg": "Error message"
},
"variant": {
"language": null,
"mediaType": {
"type": "application",
"subtype": "json",
"parameters": {},
"wildcardType": false,
"wildcardSubtype": false
},
"encoding": null,
"languageString": null
},
"annotations": [],
"mediaType": {
"type": "application",
"subtype": "json",
"parameters": {},
"wildcardType": false,
"wildcardSubtype": false
},
"language": null,
"encoding": null
}
我希望返回以下内容:
{
"code": "SOME_ERROR_CODE",
"msg": "Error message"
}
这可能吗?
从包名可以看出,javax.ws.rs.client.Entity class 仅用于客户端。在服务器端,您不需要使用它。您实际看到的是正在序列化的 Entity
对象,而不是错误。
如果要设置类型,只需在 Response.ResponseBuilder
上使用 type()
方法(调用 Response.status()
返回)。要设置正文,只需使用 entity()
方法。
return Response.status(e.getError().getCode().getStatus())
.entity(e.getError())
.type(MediaType.APPLICATION_JSON)
.build();