json 개체를 전달하는 동안 게시 메서드를 호출하려고하지만 개체를 인쇄 할 때 단위 형식으로 표시됩니다. 자원저지 서비스에 대한 POST 요청
@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();
}
덕분에
이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 + "}";
}
}
방법 : 내가 curl -X POST -H 'Content-Type: application/json' -d '{"distributors":{"distributorId":"5","name":"SA"}}' {path}
같은 일을하고
다음은 자바 코드입니다.
이상하게 뉴저지 1.3 나를 위해 완벽하게 작동을 사용하여, 아무것도 1.4 이상이 올바르게 작동하지 않는 것 같습니다. 1.3을 사용하면 내 콘솔에서'distributors : {distributorId = 5, name = SA} '를 얻습니다. – Durandal
Jersey 1.3에서도 아무 것도 바뀌지 않았습니다. – user1552879
'JAXBElement 요소 '를'Distributor distributor'로 다시 변경하면 저에게 효과적입니다. 그렇지 않으면'JSONMappingException : 적절한 생성자를 찾을 수 없습니다 .' –
Durandal