[英]405 response code on POST to my java api from POSTMAN
I have created a simple java restAPi in eclipse as dynamic web project. 我已经在eclipse中创建了一个简单的Java restAPi作为动态Web项目。 I am attempting to POST from the POSTMAN to the same endpoint(that I am doing GET) but I get 405 response code.
我试图从POSTMAN到同一端点(我正在执行GET)进行POST,但得到405响应代码。
The web.xml: web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>JsonJavaApi</display-name>
<servlet>
<servlet-name>Book API</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>test</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Book API</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
The Book class: 图书课:
public class Book {
public String author;
public String name;
public Book(String author, String name) {
super();
this.author=author;
this.name=name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Book() {
super();
}
}
The restapi code: restapi代码:
@Path("/jason")
public class JavajsonRestApi {
@GET
@Path("/json")
@Produces(MediaType.APPLICATION_JSON)
public Book getJson() {
Book book = new Book();
book.setAuthor("eswar");
book.setName("eswar");
return book;
}
}
The endpoint : 端点:
http://localhost:8080/JsonJavaApi/rest/jason/json
HTTP://本地主机:8080 / JsonJavaApi / REST /杰森/ JSON
As I understand it, the permission to post is not given to this end point but how to do I give it such that the client side can post from the body itself, this helps get rid of the 405 error? 据我了解,发布权限不是授予该端点的权限,而是我该如何做才能使客户端可以从正文本身进行发布,这有助于摆脱405错误?
Any help is appreciated. 任何帮助表示赞赏。
HTTP 405 Status codes means the method is not authorized but the url itself exists and points to a resource. HTTP 405状态码表示该方法未经授权,但URL本身存在并指向资源。 If you want to access your endpoint using
POST
method, you will have to either create another endpoint with @POST
annotation on top of it, add @POST
annotation on top of your actual endpoint (not sure this one works, never tried so far), or simply change @GET
to @POST
. 如果要使用
POST
方法访问端点,则必须创建另一个端点, @POST
在其顶部添加@POST
注释,在实际端点顶部添加@POST
注释(不确定该端点是否有效,到目前为止从未尝试过) ,或简单地改变@GET
到@POST
。
I think you've got some misunderstanding of what and how HTTP works. 我认为您对HTTP的工作方式和工作方式有一些误解。 You are making an HTTP request.
您正在发出HTTP请求。 This request might have a body (GET and DELETE methods don't have bodies).
该请求可能具有正文(GET和DELETE方法没有正文)。 The body can be gathered in Spring using
@RequestBody SomePojoClass body
as one of the parameters of your controller method. 可以使用
@RequestBody SomePojoClass body
作为控制器方法的参数之一在Spring中收集该主体。 Please see : https://baeldung.com/spring-request-response-body 请参阅: https : //baeldung.com/spring-request-response-body
Add web.xml 添加web.xml
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.sample.rest</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
And update jersey version 1.8 above 并更新球衣版本1.8以上
HTTP 405
status code means Method Not Allowed
. HTTP 405
状态代码表示Method Not Allowed
。 Since you have the method annotated with @GET
and you are trying to do the @POST
so you are getting the status code. 既然你有注释的方法
@GET
和你正在尝试做的@POST
所以你得到的状态代码。
So if you want to make a POST request to same endpoint then remove the @GET
annotation as you can have a method annotated with only one HTTP annotations at a time. 因此,如果您要向同一端点发出POST请求,请删除
@GET
注释,因为您可以使一个方法一次仅带有一个HTTP注释。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.