CustomExceptionMapper只用于Jersey中特定的API Rest。

huangapple go评论107阅读模式
英文:

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:

&lt;servlet&gt;
       &lt;servlet-name&gt;jersey-serlvet&lt;/servlet-name&gt;
       &lt;servlet-class&gt;
           org.glassfish.jersey.servlet.ServletContainer
       &lt;/servlet-class&gt;
       &lt;init-param&gt;
           &lt;param-name&gt;jersey.config.server.provider.packages&lt;/param-name&gt;
           &lt;param-value&gt;com.restapi&lt;/param-value&gt;
       &lt;/init-param&gt;
       &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
   &lt;/servlet&gt;
   &lt;servlet-mapping&gt;
       &lt;servlet-name&gt;jersey-serlvet&lt;/servlet-name&gt;
       &lt;url-pattern&gt;/api/*&lt;/url-pattern&gt;
   &lt;/servlet-mapping&gt;

I have a CustomExceptionMapper to respond with a custom message:

@Priority(1)
@Provider
public class CustomJsonMapperException implements ExceptionMapper&lt;JsonMappingException&gt; {

    @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&lt;JsonMappingException&gt; {
    @Context
    HttpServletRequest request;

    @Override
    public Response toResponse(JsonMappingException ex) {
        //return the custom message only for service &quot;api/foo&quot;
        if (request.getPathInfo().startsWith(&quot;/foo&quot;)) {
            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!

huangapple
  • 本文由 发表于 2023年3月4日 00:44:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75629748.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定