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();
}
@WebService
, which is quite high-level, but with an@WebServiceProvider
, it should be quite straightforward. Another option would be the@UsesJaxbContext
with@WebService
. Ultimately, if you're in control of the JAXBContext, you should be able to pull this off – Emanative