[英]REST API | Jersey jar | 405 error
I am trying to do automate REST API through Java. 我正在尝试通过Java自动执行REST API。 For this, I am using Jersy jar. 为此,我正在使用Jersy jar。 I googled enough and was able to write a code that doesn't throw any exception. 我用了足够的谷歌搜索,并能够编写不会引发任何异常的代码。 However, I am always getting 405 - Method not allowed error response when I try to reach my project's endpoint and post the request. 但是,当我尝试到达项目的端点并发布请求时,总是收到405-方法不允许错误响应。 Same endpoint returns success response on Soap UI. 同一端点在Soap UI上返回成功响应。 And there are no headers required. 并且没有标题。 Media type is JSON. 媒体类型为JSON。
Basically, I just want to do the SOAP UI operation, but through Eclipse. 基本上,我只想通过Eclipse进行SOAP UI操作。 PS: I'm novice, any help is appreciated. PS:我是新手,可以提供任何帮助。
package sample;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status.Family;
import org.glassfish.jersey.client.JerseyInvocation.Builder;
public class Sample
{
public static void main(String a[])
{
Client client = ClientBuilder.newClient();
WebTarget resource = client.target("https://endpoint-url/Resource");
Form form = new Form();
form.param("name", "bond");
form.param("ID", "007");
Builder request = (Builder) resource.request();
request.accept(MediaType.APPLICATION_JSON);
Response response = request.get();
if (response.getStatusInfo().getFamily() == Family.SUCCESSFUL)
{
System.out.println("Success! " + response.getStatus());
System.out.println(response.readEntity(String.class));
}
else
{
System.out.println("ERROR! " + response.getStatus());
// System.out.println(response);
System.out.println(response.readEntity(String.class));
}
}
}
The issue at accept,get resource instead of Post resource you aren't passing form parameter to resource method. 接受,获取资源而不是发布资源的问题是您没有将表单参数传递给资源方法。 That is reason getting issues.Check below 这就是出现问题的原因。请检查以下内容
public class Sample {
public static void main(String a[])
{
Client client = ClientBuilder.newClient();
WebTarget resource = client.target("https://endpoint-url/Resource");
Form form = new Form();
form.param("name", "bond");
form.param("ID", "007");
Builder request = (Builder) resource.request();
request.accept(MediaType.APPLICATION_JSON);
Response response = request.post(Entity.entity(form,MediaType.APPLICATION_FORM_URLENCODED));
if (response.getStatusInfo().getFamily() == Family.SUCCESSFUL)
{
System.out.println("Success! " + response.getStatus());
System.out.println(response.readEntity(String.class));
}
else
{
System.out.println("ERROR! " + response.getStatus()); // System.out.println(response);
System.out.println(response.readEntity(String.class));
}
}
}
Finally, I found the code here that worked wonders for me :) 最后,我在这里找到了对我有用的代码:)
Below is the working code: 下面是工作代码:
package sample;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class JavaNetURLRESTFulClient
{
private static final String targetURL = "https://endpoint-url/Resource";
public static void main(String[] args)
{
try {
URL targetUrl = new URL(targetURL);
HttpURLConnection httpConnection = (HttpURLConnection) targetUrl.openConnection();
httpConnection.setDoOutput(true);
httpConnection.setRequestMethod("POST");
httpConnection.setRequestProperty("Content-Type", "application/json");
String input = "{\"name\":\"bond\",\"Id\":007}";
OutputStream outputStream = httpConnection.getOutputStream();
outputStream.write(input.getBytes());
outputStream.flush();
if (httpConnection.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ httpConnection.getResponseCode());
}
BufferedReader responseBuffer = new BufferedReader(new InputStreamReader(
(httpConnection.getInputStream())));
String output;
System.out.println("Output from Server:\n");
while ((output = responseBuffer.readLine()) != null) {
System.out.println(output);
}
httpConnection.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.