2

Content-TypeCXF (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

あなたがしていることに何も問題はありません。少なくとも content-type の設定は機能する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 に答える