英文:
CustomExceptionMapper only for a specific API Rest with Jersey
问题
如何在Jersey 2.34中仅针对一个API服务(例如,仅针对"/api/foo")应用我的CustomJsonMapperException,而不是所有其他"/api/*"服务?
要在Jersey中仅为一个特定的API服务应用CustomJsonMapperException,你可以使用以下方法:
首先,你可以创建一个新的ExceptionMapper,用于处理"/api/foo"服务的异常情况。然后,在该Mapper中实现你的自定义逻辑。
以下是示例代码:
@Provider
public class CustomFooExceptionMapper implements ExceptionMapper<JsonMappingException> {
@Override
public Response toResponse(JsonMappingException bex) {
// 在这里实现你的自定义逻辑来处理"/api/foo"服务的异常情况
if (bex.getMessage().contains("/api/foo")) {
return Response.status(400).entity("Custom message for /api/foo").build();
} else {
// 如果不是"/api/foo"服务的异常,可以返回其他处理方式或HTTP响应
return Response.status(400).entity("Generic error message").build();
}
}
}
然后,你需要在Jersey的配置中注册这个CustomFooExceptionMapper,但只为"/api/foo"服务。这可以通过在Web.xml中进行配置来实现:
<servlet>
<servlet-name>jersey-serlvet</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.restapi</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/api/foo/*</url-pattern> <!-- 指定只为"/api/foo"服务处理异常 -->
</servlet-mapping>
这将确保CustomFooExceptionMapper仅用于处理"/api/foo"服务的异常情况,而对于其他"/api/*"服务,将继续使用通用的ExceptionMapper(如果已注册的话)。
英文:
My application has many different API REST build with Jersey 2.34. All the url starts with "/api/" following by the service's name, for example: /api/foo, /api/bar, etc.
In my web.xml, the servlet is declared:
<servlet>
<servlet-name>jersey-serlvet</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.restapi</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
I have a CustomExceptionMapper to respond with a custom message:
@Priority(1)
@Provider
public class CustomJsonMapperException implements ExceptionMapper<JsonMappingException> {
@Override
public Response toResponse(JsonMappingException bex) {
return Response.status(400).entity(MY_CUSTOM_MESSAGE).build();
}
}
How can I apply my CustomJsonMapperException only for one api service, for exemple only for "/api/foo" and not all other "/api/*"?
答案1
得分: 0
以下是翻译好的内容:
通过此线程 https://stackoverflow.com/questions/48526488/how-to-map-json-parsing-related-errors-to-json-response-in-jersey 中的一些解释,我成功实现了以下功能:
- 添加一个 HttpServletRequest 对象,以检索调用的 API 路径
- 根据调用的 API 路径过滤返回的响应:如果是需要自定义消息的 API -> 返回自定义消息,否则返回由 Jersey 和 Jackson(JsonMappingExceptionMapper)管理的默认消息。
@Priority(1)
@Provider
public class CustomJsonMappingException implements ExceptionMapper<JsonMappingException> {
@Context
HttpServletRequest request;
@Override
public Response toResponse(JsonMappingException ex) {
// 仅为服务 "api/foo" 返回自定义消息
if (request.getPathInfo().startsWith("/foo")) {
return Response.status(400).entity(MY_CUSTOM_MESSAGE).build();
} else {
// 所有其他 API 服务
return new JsonMappingExceptionMapper().toResponse(ex);
}
}
}
问题已解决,欢迎任何更正和/或改进!
英文:
With some explications from this thread https://stackoverflow.com/questions/48526488/how-to-map-json-parsing-related-errors-to-json-response-in-jersey , I managed to make it as following:
- Add a HttpServletRequest object to retrieve the api path called
- Filter the returned response based on the the api path called: if it is a API which need a custom message -> return custom message, else return the default message managed by Jersey & Jackson (JsonMappingExceptionMapper).
@Priority(1)
@Provider
public class CustomJsonMappingException implements ExceptionMapper<JsonMappingException> {
@Context
HttpServletRequest request;
@Override
public Response toResponse(JsonMappingException ex) {
//return the custom message only for service "api/foo"
if (request.getPathInfo().startsWith("/foo")) {
return Response.status(400).entity(MY_CUSTOM_MESSAGE).build();
} else {
//all others api services
return new JsonMappingExceptionMapper().toResponse(ex);
}
}
}
It is solved but any correction and/or improvement are welcome!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论