我正在尝试使用Service/Dispatch机制发布JAX-RS资源.问题是传出请求的Content-Type被锁定text/xml.例如,我无法看到将此更改为其他类型的方法application/xml.
RESTfull Web服务仅使用application/xml和application/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 …Run Code Online (Sandbox Code Playgroud)