简体   繁体   English

如何从 java soap web 服务返回 JSON 结果

[英]How to return JSON result from java soap web service

Ok guys i have come up with my test code as you suggested to do so.好的,我已经按照您的建议提出了我的测试代码。 I searched the internet and i get Gson that makes the task easy.我搜索了互联网,我得到了Gson ,这使任务变得简单。 i tested it by printing the results to the console like this and it returns the expected json result我通过像这样将结果打印到控制台来测试它并返回预期的json结果

public String printJson()
{
    ProductBLL productBLL = new ProductBLL();
    List<String> catList = productBLL.getProductCatagories();

    Gson gson = new Gson();
    String jsonCatList = gson.toJson(testList);
    System.out.println("Category List: " + jsonCatList);

}

Output输出
Category List: ["Book","Music","Movies"]

but when i try it in my java soap web service its not working that means the web service is still returning xml.但是当我在我的 java soap web 服务中尝试它时,它不起作用,这意味着 web 服务仍在返回 xml。

this is the web service method using Gson这是使用Gson的 Web 服务方法

@WebMethod
public String getCategories()
{
    List<String> catList = ppBll.getProductCatagories();

    Gson gson = new Gson();
    String jsonCatagoryList = gson.toJson(catList);

    return jsonCatagoryList ;
}


output输出

<?xml version="1.0" encoding="UTF-8"?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<S:Body>
    <ns2:getCategoriesResponse xmlns:ns2="http://aman.org/">
        <return>Book</return>
        <return>Music</return>
        <return>Movies</return>
    </ns2:getCategoriesResponse>
</S:Body>

but i need it to return json .但我需要它来返回json Where should it be modified to make it work in the web service?应该在哪里修改它以使其在 Web 服务中工作?

SOAP web service will always return XML, may be you can create another REST web service which will internally call the SOAP WS and produce a JSON. SOAP Web 服务将始终返回 XML,也许您可​​以创建另一个 REST Web 服务,它将在内部调用 SOAP WS 并生成 JSON。 with your REST you can easily do it.使用 REST,您可以轻松完成。

so Step 1:所以第一步:

create a REST WS which produces JSON创建一个生成 JSON 的 REST WS

Step 2:第2步:

Call SOAP WS from This REST WS and transform the output to the format you want.从这个 REST WS 调用 SOAP WS 并将输出转换为您想要的格式。

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

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