问题描述
好的,我已经按照您的建议提出了我的测试代码。
我搜索了互联网,我得到了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
的 Web 服务方法
@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 服务中工作?
1楼
SOAP Web 服务将始终返回 XML,也许您可??以创建另一个 REST Web 服务,它将在内部调用 SOAP WS 并生成 JSON。 使用 REST,您可以轻松完成。
所以第一步:
创建一个生成 JSON 的 REST WS
第2步:
从这个 REST WS 调用 SOAP WS 并将输出转换为您想要的格式。