简体   繁体   English

JAX-WS 网络服务的 JSON 输出?

[英]JSON output of a JAX-WS webservice?

Is it possible that a jax-ws soap-webservice can output json format instead of xml ? jax-ws soap-webservice是否有可能输出json格式而不是xml

@Component
@WebService
public class HRSService {

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

Apparently it's possible by following the instructions indicated at https://jax-ws-commons.java.net/json/ (Archive version)显然可以按照https://jax-ws-commons.java.net/json/(存档版本)中指示的说明进行操作

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.您只需要WEB-INF/lib jaxws-json.jar即可使其工作。

I hope it helps!我希望它有帮助!

This is late.这已经晚了。 I recently returned to programming in Java, but for those who will be visiting this page in the future.我最近回到了 Java 编程,但对于那些将来会访问此页面的人。 The example in the JAXWS metro documents works only in javascript. JAXWS Metro 文档中的示例仅适用于 javascript。 I used the following together with JSONObject:我将以下内容与 JSONObject 一起使用:

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

then implement Provider(DataSource), as in example:然后实现 Provider(DataSource),如示例所示:

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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM