我正在尝试在传递json对象时调用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();
}
感谢。
答案 0 :(得分:0)
我所做的就是更改返回类型:
@POST
@Path("/distpost")
@Produces(MediaType.APPLICATION_JSON)
public Response addDistributor(final Distributor distributor) {
System.out.println(distributor);
return Response.status(201).entity(distributor.toString()).build();
}
它处理这样的卷曲测试:
curl -X POST -H 'Content-Type: application/json' -d '{"distributor": {"distributorId":5,"name":"SA"}}' <url>