JSON output of JAX-WS web service?

Is it possible what the jax-ws

soap-webservice

format might output json

instead xml

?

@Component
@WebService
public class HRSService {

    @WebMethod
    public String test(String value) {
        return value; //returned as XML. JSON possible?
    }
}

      

+3


source to share


2 answers


Apparently this is possible by following the instructions given at https://jax-ws-commons.java.net/json/

Summarizing:

@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 yours WEB-INF/lib

for this to work.

Hope this helps!

+4


source


It's already late. I recently returned to Java programming, but for those who will visit this page in the future. The example in the JAXWS metro docs only works in javascript. I used the following along with JSONObject:

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

      



then implements Provider (DataSource) as in the 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();
}

      

+1


source







All Articles