[英]Submit JSON data as request body in an Apache CXF REST Service
[英]How to access JSON request body of POST/PUT Request in Apache Camel/CXF REST webservice
我正在尝试将JSON请求正文传递给在我的Apache Camel应用程序中使用CXFRS制成的REST Web服务。
我想访问在处理器中传递的请求JSON。
REST URL:
http://localhost:8181/mywebservice/Hello/name/{request_param}
虽然我在请求正文中发布了JSON,但仍在我的处理器中exchange.getIn().getBody()
始终返回{request_param}
而不是Request JSON。
我的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);
}
}
服务器部分:
@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);
}
客户部分:
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.