【问题标题】:How to return JSON result from java soap web service如何从 java soap web 服务返回 JSON 结果
【发布时间】:2019-07-25 16:05:00
【问题描述】:

好的,伙计们,我已经按照您的建议提出了我的测试代码。我搜索了互联网,我得到了Gson,这使任务变得容易。我通过像这样将结果打印到控制台来测试它,它返回预期的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);

}

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

但是当我在我的 java soap web 服务中尝试它时它不起作用,这意味着 web 服务仍在返回 xml。

这是使用Gson的网络服务方法

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

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

    return jsonCatagoryList ;
}


输出

<?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>

但我需要它返回json。应该在哪里修改以使其在 Web 服务中工作?

【问题讨论】:

  • 是的,有办法,你为什么不自己试试呢? Stackoverflow 不是免费的 c# 到 java 转换器。请自己做一些研究,尝试自己编写一些代码,如果您有任何问题,您可以回来询问有关特定代码的特定问题
  • @Lino 正如你所说,我已经做了一些研究,但我没有找到我所看到的,这就是我来这里的原因。我什至见过类似的问题,但作为初学者,它们对我没有帮助......
  • 这不是问题。这是一篇“为我做”的帖子...

标签: java json web-services soap gson


【解决方案1】:

SOAP Web 服务将始终返回 XML,也许您可​​以创建另一个 REST Web 服务,该服务将在内部调用 SOAP WS 并生成 JSON。使用您的 REST,您可以轻松完成。

所以 第 1 步:

创建一个生成 JSON 的 REST WS

第 2 步:

从此 REST WS 调用 SOAP WS 并将输出转换为您想要的格式。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-04-08
  • 2012-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多