简体   繁体   English

如何在Apache Camel / CXF REST Web服务中访问POST / PUT请求的JSON请求正文

[英]How to access JSON request body of POST/PUT Request in Apache Camel/CXF REST webservice

I am trying to pass a JSON request body to a REST Webservice which is made using CXFRS in my Apache Camel application. 我正在尝试将JSON请求正文传递给在我的Apache Camel应用程序中使用CXFRS制成的REST Web服务。

I want to access the request JSON passed in my Processor. 我想访问在处理器中传递的请求JSON。

REST URL: REST URL:

http://localhost:8181/mywebservice/Hello/name/{request_param}

Though i am posting a JSON in request body, still in my processor exchange.getIn().getBody() always return the {request_param} not the Request JSON. 虽然我在请求正文中发布了JSON,但仍在我的处理器中exchange.getIn().getBody()始终返回{request_param}而不是Request JSON。

My REST webservice is as follows: 我的REST Web服务如下:

@Path("/name/")
@Consumes({"application/json" ,"application/xml"})
public class HelloRest {
    @POST
    @Path("/{name}")
    public TestPojo sayHi(@PathParam("name") String name) {
        return new TestPojo(name);
    }
}

Server part: 服务器部分:

    @POST
@Path("/")
@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.APPLICATION_JSON)
public String add(MappingUser newUser){
    UserEntity userEntity = new UserEntity(newUser.getNickname(), newUser.getPassword());
    boolean ret = myDB.addUser(userEntity);

    //sends the return value (primitive type) as plain text over network
    return String.valueOf(ret);
}

Client part: 客户部分:

 public boolean addUser(User user){
    WebResource resource = client.resource(url).path("/");

    String response = resource
            //type of response
            .accept(MediaType.TEXT_PLAIN_TYPE)
            //type of request
            .type(MediaType.APPLICATION_JSON_TYPE)
            //method
            .post(String.class, user);

    return Boolean.valueOf(response);
}

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

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