简体   繁体   English

从POSTMAN到我的Java API的POST上的405响应代码

[英]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

EDIT 编辑

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.

相关问题 如何使用 Postman 发布到 Java 中的 API? - How to POST with Postman to an API in Java? 在Java中将状态405发送到api - getting status 405 in java post to api Sending POST request with POSTMAN to Java REST API and getting null values in the xml response - Sending POST request with POSTMAN to Java REST API and getting null values in the xml response 当我将 json 字段发布到我的 api 时,postman 检索到空响应 - postman retrieve an empty response when i post json field to my api Java 中的 403 响应代码,但在 Postman 中有效 - 403 response code in Java, but works in Postman 为什么我收到“不支持请求方法 &#39;POST&#39;”? 我在邮递员上收到错误代码 405 - Why am I receiving a "Request method 'POST' not supported" ? I get error code 405 on postman 放心的代码在Java API中给出503响应,但在POSTMAN中可以正常工作 - Rest assured code giving 503 response in java API but working fine in POSTMAN 周四在 java 代码中调用 Rest API 时得到空白响应,但周四邮递员工作正常 - Getting blank response on invoking Rest API thur java code but thur postman working fine java.io.IOException:CONNECT的意外响应代码:405 - java.io.IOException: Unexpected response code for CONNECT: 405 在REST API中使用HttpURLConnection到Java中的POST时,响应代码为400 - Response code is 400 while using HttpURLConnection to POST in java for REST API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM