简体   繁体   English

无法使用Postman将POST JSON请求发送到Restful API

[英]Unable to send POST json request to restful api using Postman

I have a RESTful api as follow : 我有一个RESTful api,如下所示:

@Path("/entity")
public class EntityService {

    @GET
    @Path("/create")
    public Response createEvent(){
        return Response.ok("Entity created successfully").build();
    }

    @POST
    @Path("/test")
    @Consumes(MediaType.APPLICATION_JSON)
    public Response testPost(Book book){
        System.out.println(book.toString());
        return Response.ok("Testing Post").build();
    }

    /**
     * Book have an id and name
     */
    public class Book {
        public String id;
        public String name;

        public Book(String id, String name) {
            this.id = id;
            this.name = name;
        }

        @Override
        public String toString() {
            return id + ", " + name;
        }
    }
}

I am able to send GET request to localhost:8080/entity successfully using POSTMAN. 我能够使用POSTMAN将GET请求成功发送到localhost:8080 / entity。

But don't know how to send POST request. 但是不知道如何发送POST请求。 I have made request using postman as shown in image. 我已经使用邮递员提出了要求,如图所示。

请求

And I got this response 我得到了这个回应

效应初探

Looking at your dependencies, I see you have the core Jackson dependencies. 查看您的依赖关系,我发现您拥有核心的Jackson依赖关系。 Jackson core alone isn't enough though. 仅仅杰克逊核心还不够。 You need the Jackson JAX-RS JSON provider. 您需要Jackson的JAX-RS JSON提供程序。 This is included in 这包括在

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-json</artifactId>
    <version>1.12</version>
</dependency>

You should also get rid of all the Jackson dependencies you currently have. 您还应该摆脱当前拥有的所有Jackson依赖项。 The above will pull them in (you don't want versions to conflict). 上面的代码将它们拉入(您不希望版本冲突)。

Then you need to configure Jersey to use Jackson by adding the POJO mapping feature configuration switch, in your web.xml 然后,您需要通过在web.xml中添加POJO映射功能配置开关来配置Jersey以使用Jackson。

<servlet>
    <servlet-name>...</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
        <param-value>true</param-value>
    </init-param>
       ...
</servlet>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM