如何使用Jersey REST处理服务不可用的情况 [英] How to handle Service unavailable scenarios with Jersey REST

查看:101
本文介绍了如何使用Jersey REST处理服务不可用的情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Spring集成的jersey RESTful服务.web.xml中映射的基本URL是/rest/* 我的服务等级如下:

I have a jersey RESTful service integraed with Spring.The base url mapped in web.xml is /rest/* My service class is below:

@Resource
@Scope("request")
@Path("/service/")
@Component
public class ServiceController {

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Response getApiDetails() {
        return Response.ok("area").build();
    }

    @Path("/area")
    @GET
    @Consumes({ MediaType.APPLICATION_JSON })
    @Produces(MediaType.APPLICATION_JSON)
    public Response processConvertCurrency(
        @QueryParam("length") String length,
        @QueryParam("breadth") String breadth) {
        ......
    }
}

点击以下网址后,它将显示可用的服务

When the following url hit, it will show the available services

<baseurl>/rest/service

output : area

<baseurl>/rest/service/area?length=10&breadth20也将正确返回其输出.

Also <baseurl>/rest/service/area?length=10&breadth20 will return its output correctly.

我的要求是,当有人按下<baseurl>/rest/service/volume时,其输出应类似于

My requirement is that when someone hit <baseurl>/rest/service/volume it should output like

Service is unavailable message.

由于只有area可用,我该怎么办.请帮忙.(当前显示404错误)

Since only area is available How can I do that.Please help..(currently 404 error is displayed)

推荐答案

如果找不到该资源,JAX-RS(泽西岛)将抛出NotFoundException.此异常将映射到包含状态代码404 Not Found(没有实体主体)的响应,因为这是正常的REST行为.但是,我们也可以通过创建

JAX-RS (Jersey) will throw a NotFoundException if the resource isn't found. This exception will be mapped to a response containing a status code 404 Not Found (with no entity body), as this is normal REST behavior. But we are also allowed to change the response to our liking by creating an ExceptionMapper. There we can set an entity body. You might have something like

import javax.ws.rs.NotFoundException;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

@Provider
public class NotFoundExceptionMapper
        implements ExceptionMapper<NotFoundException> {
    final String html
            = "<p style='font-size:20px;font-family:Sans-serif'>"
            + "Service is unavailable message."
            + "</p>"
            + "<a href='http://www.stackoverflow.com'>"
            + "Check out Stackoverflow</a>";

    @Override
    public Response toResponse(NotFoundException e) {
        return Response.status(Status.NOT_FOUND)
                .entity(html).build();
    }
}

我们只需要在Jersey(JAX-RS)应用程序中注册此提供程序(可能通过ResourceConfig).

We just need to register this provider with the Jersey (JAX-RS) application (possibly through the ResourceConfig).

似乎只要有响应的实体,我们就会得到

It seems as long as we have a entity body with the response, we'll get

代替

  • 请参阅异常映射
  • 注意:您可以通过web.xml轻松地将404映射到您选择的错误页面,而无需任何ExceptionMapper.像

  • See Exception Mapping
  • Note: you could just as easily simply map 404 to an error page of your choosing, through the web.xml, without the need for any ExceptionMapper. Something like

<error-page>
    <error-code>404</error-code>
    <location>/error.html</location>
</error-page>

有关详细信息,请参见此处

当我为所有404条相同的消息设置时,还有一个疑问.我们是否也可以基于请求url或其他方式进行自定义(我的目的是为不同的服务提供不同的消息)"

"One more doubt, when I set it, for all 404 same message. Can we customize too based on the request url or some otherway.(My intention is to provide different message for different services)"

您可以将不同的上下文注入到ExceptionMapper中.例如 UriInfo ,并获取请求的路径

You can inject different contexts into the ExceptionMapper. For instance UriInfo, and get the requested path

@Context
UriInfo uriInfo;

@Override
public Response toResponse(NotFoundException e) {
    String html
        = "<p style='font-size:20px;font-family:Sans-serif'>"
        + "Service is unavailable message."
        + "</p>"
        + "<a href='http://www.stackoverflow.com'>"
        + "Check out Stackoverflow</a>";
    html += "<p>Requested URI: " + uriInfo.getAbsolutePath() + "</p>";
    return Response.status(Status.NOT_FOUND)
            .entity(html).build();
}

还有其他可以注入的上下文.它们是否可注入到类中取决于类(资源类/提供者类)及其应用范围(除其他外).但这是可注入上下文的列表(带有@Context注释).

There are other contexts that can be injected. Whether they are inject-able into a class depends on the class (resource class / provider class) and its application scope (among other things). But here's a list of inject-able contexts (with the @Context annotation).

  • javax.ws.rs.core.HttpHeaders
  • javax.ws.rs.core.UriInfo
  • javax.ws.rs.core.Request
  • javax.servlet.HttpServletRequest
  • javax.servlet.HttpServletResponse
  • javax.servlet.ServletConfig
  • javax.servlet.ServletContext
  • javax.ws.rs.core.SecurityContext
  • 可能更多:-)
  • javax.ws.rs.core.HttpHeaders
  • javax.ws.rs.core.UriInfo
  • javax.ws.rs.core.Request
  • javax.servlet.HttpServletRequest
  • javax.servlet.HttpServletResponse
  • javax.servlet.ServletConfig
  • javax.servlet.ServletContext
  • javax.ws.rs.core.SecurityContext
  • Possible more :-)

这篇关于如何使用Jersey REST处理服务不可用的情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆