Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAX-WS For Json request and response [duplicate]

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?
    }
}
like image 827
membersound Avatar asked Sep 04 '14 08:09

membersound


2 Answers

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!

like image 153
Musikolo Avatar answered Oct 19 '22 07:10

Musikolo


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();
}
like image 1
Carlos H. Raymundo Avatar answered Oct 19 '22 06:10

Carlos H. Raymundo