Java Rest API客户端-获取方法-错误415
我不熟悉使用ApacheCXF为RESTfulAPI编写Java客户机 在运行下面的代码时,我得到了返回的错误415,当我在线查看时显示为“不支持的媒体类型”。为了修复它,我将原始target.request()的代码更改为“target.request(MediaType.APPLICATION_XML)”。然而,这并没有修复代码 调试此问题的最佳方法是什么? 非常感谢您抽出时间 更新:与RESTAPI开发人员讨论后,我知道我需要添加一个标题“(“内容类型”,“应用程序/x-www-form-urlencoded”);”。但我不知道如何添加标题。有人知道如何在这里添加此标题吗Java Rest API客户端-获取方法-错误415,java,apache,rest,cxf,cxf-client,Java,Apache,Rest,Cxf,Cxf Client,我不熟悉使用ApacheCXF为RESTfulAPI编写Java客户机 在运行下面的代码时,我得到了返回的错误415,当我在线查看时显示为“不支持的媒体类型”。为了修复它,我将原始target.request()的代码更改为“target.request(MediaType.APPLICATION_XML)”。然而,这并没有修复代码 调试此问题的最佳方法是什么? 非常感谢您抽出时间 更新:与RESTAPI开发人员讨论后,我知道我需要添加一个标题“(“内容类型”,“应用程序/x-www-form-
package com.blackhawk.ivr.restAPI.client;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
public class BlissRestAPI {
public static final String BLISS_SERVICRE_URL = "http://x.x.x.x:9090/services";
public static void main(String[] args) {
Client client = ClientBuilder.newClient();
WebTarget target = client.target(BLISS_SERVICRE_URL);
target = target.path("/cardmanagementservices/v3/card/status").queryParam("ani", "xxxxxxxxxx").queryParam("card.expiration", "xxxxxx").queryParam("card.number", "xxxxxxxxxxxxxxxx").queryParam("channel.id", "xyz");
Invocation.Builder builder = target.request(MediaType.APPLICATION_XML);
Response response = builder.get();
System.out.println(response.getStatus());
response.close();
client.close();
}
}首先,您可以按如下所示更改介质类型
- 客户端:MediaType.APPLICATION\u XML
- Rest:MediaType.APPLICATION_JSON
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import javax.xml.rpc.ParameterMode;
public class axisClient {
public static void main(String [] args) throws Exception {
String endpoint = "http://localhost:8090/archive_name/service_name.jws";
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress( new java.net.URL(endpoint) );
call.setOperationName( "service_method_name" );
call.addParameter("parameter_name", XMLType.XSD_STRING, ParameterMode.IN );
call.setReturnType( XMLType.XSD_STRING );
call.setProperty(Call.CHARACTER_SET_ENCODING, "UTF-8");
String jsonString = (String) call.invoke( new Object [] { "parameter_value"});
System.out.println("Got result : " + jsonString);
}
}
我使用下面的代码让它工作(返回200个状态)
如果您尝试:MediaType.TEXT\u PLAIN,它是否成功执行?谢谢Alex。我试过了,但没有成功->target.request(MediaType.TEXT\u PLAIN);
WebClient client = WebClient.create(BLISS_SERVICRE_URL);
client.path("/cardmanagementservices/v3/card/status").query("ani", "xxxxxxxxxx").query("card.expiration", "xxxxxx").query("card.number", "xxxxxxxxxxxxxx").query("channel.id", "xxxxx");
client.type(MediaType.APPLICATION_FORM_URLENCODED).accept(MediaType.APPLICATION_XML);
client.header("Content-Type","application/x-www-form-urlencoded");
Response response = client.get();
System.out.println(response.getStatus());