我正在使用Jersey客户端实现ReST api。 ReST在POSTMAN中运行良好。它需要2个表单数据参数。
以下是我的代码。
package demo;
import java.net.URI;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;
import org.glassfish.jersey.client.ClientConfig;
public class ReSTCallToICRT {
private static final String webServiceURI = "my-post-uri";
public static void main(String[] args) {
ClientConfig clientConfig = new ClientConfig();
Client client = ClientBuilder.newClient(clientConfig);
URI serviceURI = UriBuilder.fromUri(webServiceURI).build();
WebTarget webTarget = client.target(serviceURI);
Form form = new Form();
form.param("parameter1", "value1");
form.param("parameter2", "value2");
String callResult = webTarget.request(MediaType.TEXT_PLAIN).post(Entity.entity(form,MediaType.MULTIPART_FORM_DATA_TYPE),String.class);
System.out.println(callResult);
}
}
在eclipse中作为Java项目运行时,我收到以下错误
Exception in thread "main" javax.ws.rs.InternalServerErrorException: HTTP 500 Internal Server Error
at org.glassfish.jersey.client.JerseyInvocation.convertToException(JerseyInvocation.java:1020)
at org.glassfish.jersey.client.JerseyInvocation.translate(JerseyInvocation.java:816)
at org.glassfish.jersey.client.JerseyInvocation.access$700(JerseyInvocation.java:92)
at org.glassfish.jersey.client.JerseyInvocation$2.call(JerseyInvocation.java:700)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:444)
at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:696)
at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:448)
at org.glassfish.jersey.client.JerseyInvocation$Builder.post(JerseyInvocation.java:349)
at demo.ReSTCallToICRT.main(ReSTCallToICRT.java:31)
我在这里错过了什么吗?