在jax-rs REST服务中更改内容类型 [英] Changing content type in jax-rs REST service

查看:112
本文介绍了在jax-rs REST服务中更改内容类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请原谅我,但我可能不熟悉正确提出这个问题所需的所有术语。

Forgive me, but I may not be familiar with all the lingo necessary to ask this question properly.

我正在使用 org.apache.cxf.jaxrs.ext jax-rs的实现。方法标题如下:

I'm working on a fairly simple REST web service in Java using the org.apache.cxf.jaxrs.ext implementation of jax-rs. The method header is like this:

@GET
@Path("json/{fullAlias}")
@Produces({"application/json"})
public String json(@PathParam("fullAlias") String fullAlias, @Context MessageContext req)

其中MessageContext是 org.apache.cxf.jaxrs.ext.MessageContext

where MessageContext is org.apache.cxf.jaxrs.ext.MessageContext.

我正试图完成的两件事似乎无法弄清楚:

There are two things I'm trying to accomplish that I can't seem to figure out:


  1. 如果满足某些条件,则更改内容类型(例如,出错)

  2. 更改回复的状态代码

我尝试过通过MessageContext访问更改响应:

I've tried using changing the response by accessing it through the MessageContext:

HttpServletResponse response = req.getHttpServletResponse();
response.setContentType("text/plain")
response.setStatus("HttpServletResponse.SC_BAD_REQUEST);

但这些更改与发送的响应没有关系;无论有没有@Produces注释,设置方法内的内容类型都不会影响实际的内容类型(使用注释,它当然会返回application / json,没有它默认为text / html)。

But these changes have no bearing on the response sent; with or without the @Produces annotation, setting the content type inside the method doesn't affect the actual content type (With the annotation, it of course returns "application/json", without it defaults to "text/html").

我正在返回一个简单的字符串作为正文。我试图返回一个javax.ws.rs.core.Response对象做我想做的事,但我对它不太了解。

I am returning a simple String as the body. I've entertained trying to return a javax.ws.rs.core.Response object to do what I want, but I don't know much about it.

我如何更改内容类型和/或者这个方法里面的状态代码?

How would I change the content type and/or the status codes from inside this method?

推荐答案

一种方法是抛出一个WebApplicationException,如Pace所描述的,它将起作用如果您希望专门处理错误情况。如果您希望能够出于任何原因随时更改您的内容,如果你的服务方法而不是String,你会想看看返回一个Response。返回响应可以让您对服务如何响应客户端请求进行最大程度的控制(它需要的代码多于返回简单字符串的代码)。

One approach is to throw a WebApplicationException, as described by Pace, which will work if you are looking to specifically handle an error condition. If you are looking to be able to change your content at any time for any reason, then you will want to take a look at returning a Response as the result of your service method rather than a String. Returning a Response gives you the greatest amount of control over how your service responds to the client request (it does require more code than returning a simple string).

以下是如何使用Response对象的示例:

Here is an example of how you would can make use of the Response object:

@GET
@Path("json/{fullAlias}")
public Response json(@PathParam("fullAlias") String fullAlias, @Context MessageContext req) {
    ...
    if (success) {
        ResponseBuilder rBuild = Response.ok(responseData, MediaType.APPLICATION_JSON);
        return rBuild.build();
    }
    else {
        ResponseBuilder rBuild = Response.status(Response.Status.BAD_REQUEST);
        return rBuild.type(MediaType.TEXT_PLAIN)
                     .entity("error message")
                     .build();
    }    
}

这篇关于在jax-rs REST服务中更改内容类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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