0
我想在传递json对象时调用post方法,但是当我打印对象时它是以单位形式。POST请求泽西服务
我做这样的事情 curl -X POST -H 'Content-Type: application/json' -d '{"distributors":{"distributorId":"5","name":"SA"}}' {path}
这里的Java代码:
package com.rest.resource;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "distributors")
public class Distributor
{
@XmlElement
private long distributorId;
@XmlElement
private String name;
public Distributor()
{
}
public Distributor(final long distributorId,final String name)
{
this.distributorId = distributorId;
this.name = name;
}
@Override
public String toString()
{
return "distributors: {distributorId = " + distributorId + ", name = " + name + "}";
}
}
方法在资源
@POST
@Path("")
public Response addDistributor(final JAXBElement<Distributor> element)
{
final Distributor distributor = element.getValue();
System.out.println(distributor);
return Response.status(200).entity(distributor.toString()).build();
}
感谢。
奇怪的是使用泽西1.3它完美对我来说,什么1.4或在它上面似乎没有正常工作。使用1.3,我在我的控制台中获得'发行商:{distributorId = 5,name = SA}'。 – Durandal
即使泽西岛1.3也没有任何变化.. – user1552879
如果我将'JAXBElement element'更改为'分销商分销商',它对我有用。否则,我会得到:'JSONMappingException:找不到合适的构造函数' –
Durandal