[英]POST request to Jersey Service
I am trying to call a post method while passing a json object but when I print the object it is in unitialized form. 我试图在传递json对象时调用post方法,但是当我打印该对象时,它采用统一形式。
I am doing something like curl -X POST -H 'Content-Type: application/json' -d '{"distributors":{"distributorId":"5","name":"SA"}}' {path}
我正在做类似curl -X POST -H 'Content-Type: application/json' -d '{"distributors":{"distributorId":"5","name":"SA"}}' {path}
Here's the Java code: 这是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 + "}";
}
}
Method in the Resource 资源中的方法
@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();
}
Thanks. 谢谢。
All I did was change the return type like this: 我所做的就是更改返回类型,如下所示:
@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();
}
And it handles a curl test like this: 它处理如下的卷曲测试:
curl -X POST -H 'Content-Type: application/json' -d '{"distributor": {"distributorId":5,"name":"SA"}}' <url>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.