Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAX-WS Dispatch Request setting Content-Type

I'm trying to POST a JAX-RS resource using the Service / Dispatch mechanism. The problem is that the Content-Type of the outgoing request is locked to text/xml. I can't see a way changing this to some other kind of type, for example application/xml.

The RESTfull webservice consumes only application/xml and application/json. This is the code I use:

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));
}

The Accept header is modified to zoo, foo header is added with value bar, but Content-Type remains unchanged. I think I could use a filter and modify the Content-Type based on some conditions, or even based on foo header but this seems counter intuitive.

Here are all the headers of the request:

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

Any help is appreciated.

like image 648
Florin Avatar asked Dec 17 '12 08:12

Florin


1 Answers

You must use Service.Mode.MESSAGE instead of Service.Mode.PAYLOAD if you want to customize the entire message.

Content-Type is a message-related property.

like image 82
Nicola Ferraro Avatar answered Oct 23 '22 17:10

Nicola Ferraro