2

Content-Type当我在 CXF (v2.6.3) 内进行自己的渲染时,我在设置时遇到问题@WebMethod

以下模式工作正常:

@Path("/foo")
@WebService
public class FooService {
    @Path("/bar")
    @Produces({ "text/plain" })
    @GET
    @WebMethod
    public String bar() {
        return "hi";
    }

这将返回带有我所期望"hi"的标头的 http-client 。Content-Type: Content-Type: text/plain

但是,当我尝试使用 response 进行自己的渲染时OutputStream"hi"会正确返回,但@Produces会忽略注释并返回默认的text/xml内容类型。即使我打电话给setContentType(...)自己也是如此。

@Path("/heartbeat2")
@Produces({ "text/plain" })
@WebMethod
@Get
public void heartbeat2() {
    HttpServletResponse response = messageCtx.getHttpServletResponse();
    response.getOutputStream().write("hi".getBytes());
    // fails with or without this line
    response.setContentType("text/plain");
}

这是输出:

HTTP/1.1 200 OK
Content-Type: text/xml
Content-Length: 2
Connection: keep-alive
Server: Jetty(8.1.9.v20130131)

hi

知道如何将自己的输出直接渲染到输出流适当地设置内容类型吗?提前致谢。

4

1 回答 1

3

我看不出你在做什么有什么问题。至少设置内容类型HttpServletResponse应该可以工作。无论如何,如果您使用javax.ws.rs.core.Response. 看看这是否有效:

@Path("/foo")
@WebService
public class FooService {
    @Path("/bar")
    @GET
    @WebMethod
    public Response bar() {
        return Response.ok().type("text/plain").entity("hi").build();
    }
    ...
于 2013-06-01T16:09:27.440 回答