6

我正在尝试使用服务/调度机制发布 JAX-RS 资源。问题是传出请求的 Content-Type 被锁定为text/xml. 例如,我看不到将其更改为其他类型的方法application/xml

RESTfull Web 服务仅使用application/xmlapplication/json。这是我使用的代码:

public static void main(String[] args) {
    QName qName = new QName("GREETINGS");

    Service service = Service.create(qName);
    service.addPort(qName, HTTPBinding.HTTP_BINDING, "http://localhost:8081/gf-ws-1/resources/greetings");

    // change headers of the outgoing request
    Map<String, Object> headers = new HashMap<String, Object>();
    headers.put("Content-Type", Arrays.asList(new String[] {"application/xml"}));
    headers.put("Accept", Arrays.asList(new String[] {"zoo"}));
    headers.put("foo", Arrays.asList(new String[] {"bar"}));

    Dispatch<Source> dispatch = service.createDispatch(qName, Source.class, Service.Mode.PAYLOAD);
    dispatch.getRequestContext().put(MessageContext.HTTP_REQUEST_METHOD, "POST");
    dispatch.getRequestContext().put(MessageContext.HTTP_REQUEST_HEADERS, headers);

    dispatch.invokeOneWay(new StreamSource(new StringReader("<?xml version='1.0' encoding='UTF-8'?><greeting><value>Hello World!</value></greeting>")));

    // get the response code: [HTTP/1.1 415 Unsupported Media Type] because of content type
    System.out.println(((Map<String, Object>) dispatch.getResponseContext().get(MessageContext.HTTP_RESPONSE_HEADERS)).get(null));
}

Acceptheader 被修改为,header 添加了 value zoo,但保持不变。我想我可以使用过滤器并根据某些条件修改,甚至基于 foo 标头,但这似乎违反直觉。foobarContent-TypeContent-Type

以下是请求的所有标头:

POST /gf-ws-1/resources/greetings HTTP/1.1
Accept: zoo
Content-Type: text/xml
foo: bar
User-Agent: Metro/2.2.0-1 (tags/2.2.0u1-7139; 2012-06-02T10:55:19+0000) JAXWS-RI/2.2.6-2 JAXWS/2.2 svn-revision#unknown
Host: localhost:8080
Connection: keep-alive
Content-Length: 86

任何帮助表示赞赏。

4

1 回答 1

1

如果要自定义整个消息,则必须使用 Service.Mode.MESSAGE 而不是 Service.Mode.PAYLOAD。

Content-Type 是与消息相关的属性。

于 2014-12-06T23:09:27.787 回答