4

Is it possible that a jax-ws soap-webservice can output json format instead of xml?

@Component
@WebService
public class HRSService {

    @WebMethod
    public String test(String value) {
        return value; //returned as XML. JSON possible?
    }
}
membersound
  • 81,582
  • 193
  • 585
  • 1,120
  • 1
    I don't think so. SOAP protocol have many featurs - WS-security, for example. I don't know haw it can work with JSON onstead of XML. But of course you can write some REST service and do marshalling as you want. – user1516873 Sep 04 '14 at 08:43
  • 1
    It might be possible with CXF, but with some drawbacks explained here: http://cxf.apache.org/docs/jax-rs-and-jax-ws.html – Magic Wand Sep 04 '14 at 09:14
  • It should be possible. JAXB is capable of marshalling and unmarshalling JSON using the Moxy implementation. I can't say it'll work smoothly with an `@WebService`, which is quite high-level, but with an `@WebServiceProvider`, it should be quite straightforward. Another option would be the [`@UsesJaxbContext`](http://stackoverflow.com/q/5627173/1530938) with `@WebService`. Ultimately, if you're in control of the JAXBContext, you should be able to pull this off – kolossus Sep 04 '14 at 16:19
  • Possible duplicate of [JAX-WS For Json request and response](https://stackoverflow.com/questions/32990357/jax-ws-for-json-request-and-response) – Vadzim Apr 17 '19 at 14:41
  • 2
    @Vadzim If you are suggesting duplicates, make sure that the question actually has worthwhile answers (and not only link-only answers). I have reversed the direction of the vote. – Mark Rotteveel Apr 17 '19 at 16:59

2 Answers2

5

Apparently it's possible by following the instructions indicated at https://jax-ws-commons.java.net/json/ (Archive version)

Summing up:

@BindingType(JSONBindingID.JSON_BINDING)
public class MyService {

    public Book get(@WebParam(name="id") int id) {
        Book b = new Book();
        b.id = id;
        return b;
    }

    public static final class Book {
        public int id = 1;
        public String title = "Java";
    }
}

You just need jaxws-json.jar in your WEB-INF/lib for this to work.

I hope it helps!

buergi
  • 6,039
  • 3
  • 19
  • 15
Musikolo
  • 51
  • 1
  • 3
1

This is late. I recently returned to programming in Java, but for those who will be visiting this page in the future. The example in the JAXWS metro documents works only in javascript. I used the following together with JSONObject:

@WebServiceProvider
@ServiceMode(value = Service.Mode.MESSAGE)
@BindingType(value=HTTPBinding.HTTP_BINDING)

then implement Provider(DataSource), as in example:

public class clazz implements Provider<DataSource>
{ ...

    @Override
    public DataSource invoke(DataSource arg)
    { 
        ...
        String emsg = "Request Parameter Error.";
        String sret = create_error_response(emsg);

        return getDataSource(sret);
    }
}

private DataSource getDataSource(String sret)
{
    ByteArrayDataSource ds = new ByteArrayDataSource(sret.getBytes(), "application/json");
    return ds;
}

public String create_error_response(String msg)
{
    JSONObject json = new JSONObject();
    json.put("success", new Boolean(false));
    json.put("message", msg);
    return json.toString();
}