WebService:返回Json数据时通过Java该如何读取?

以往使用SoapUI测试时,返回的都是如下的XML格式:

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Body>
      <ns2:printResponse xmlns:ns2="http://server.com/">
         <return>Printing:</return>
      </ns2:printResponse>
   </S:Body>
</S:Envelope>

这回调用接口返回的却是这样的Json格式

{
  "expire": "3600",
  "custom_token": "992A5AF42EC119B50D9D6F7E25CF85BE"
}<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><getkeyResponse xmlns="http://tempuri.org/" /></soap:Body></soap:Envelope>

请问这样的返回结果通过CXF等类库该如何调用并解析呢?

JaxWsDynamicClientFactory jaxWsDynamicClientFactory = JaxWsDynamicClientFactory.newInstance();
Client client = jaxWsDynamicClientFactory.createClient("https://xxxxxx/api.asmx?wsdl");

通过上面的方式似乎只能调用并解析XML类型的返回结果

================================使用Postman发送如下请求即可====================================

clipboard.png

赋上示例Java代码

List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
params.add(new BasicNameValuePair("username", "admin"));
params.add(new BasicNameValuePair("password", "123456"));
HttpPost httpPost = new HttpPost("https://www.custom.com.hk/sg8000/api.asmx/gettoken?corpid");
httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
httpPost.setHeader("Content-type", "application/x-www-form-urlencoded");
 
HttpClient httpClient = HttpClients.createDefault();
HttpResponse response = httpClient.execute(httpPost);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200) {
    HttpEntity httpEntity = response.getEntity();
    String str = EntityUtils.toString(httpEntity, "UTF-8");
}
阅读 4.5k
1 个回答

JSON字符串转java类,随便某个json框架都行,fastjson,Jackson,GSON都有相应的方法

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题