[英]how to send 2 PoJos in the POST request body using jersey?
我想使用球衣发送带有POST的Pojo。
我了解泽西岛有2个版本:1 ...和2 ...
我正在将代码添加到使用以下内容的现有项目中:
compile('com.sun.jersey:jersey-bundle:1.19')
compile('com.sun.jersey:jersey-json:1.19')
compile group: 'org.codehaus.jackson', name:'jackson-jaxrs', version: '1.1.1'
我尝试了几种方法在POST请求正文中发送2个PoJo。
但他们都不起作用。
public void acquireDevice1(Device device, int port) throws InvalidArgumentException {
WebResource webResource = this.client.resource(this.rootUri);
webResource.type(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.post(Device.class, device)
.post(Integer.class, port);
}
和
@Override
public void acquireDevice(Device device, int port) throws InvalidArgumentException {
ObjectMapper mapper = new ObjectMapper();
String deviceString = null;
try {
deviceString = mapper.writeValueAsString(device);
} catch (IOException e) {}
Client client = Client.create();
WebResource webResource = client.resource(rootUri + "/devices/acquireDevice/");
ClientResponse response = webResource
.type("application/json")
.post(ClientResponse.class, deviceString);
if (response.getStatus() != 201) {
throw new RuntimeException("Failed : HTTP error code : "
+ response.getStatus());
}
}
我应该将其全部包装在map<string,object>
吗?
发布操作是不可组合的,每个操作都映射到一个Web请求,以支持您的用例,您需要使用将它们结合在一起的第三个pojo将两个Pojo组合为一个pojo,即DeviceWithPort(或您建议的映射-尽管那样可读性差或元组)。
Jersey1 / 2在这方面没有任何区别,如上所述,这是HTTP的一部分,因此没有客户端可能会做任何不同的事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.