开发泽西休息网络服务。我需要以下面的格式发送json输出。我还粘贴了下面的java对象。但我的代码无法将我的java对象转换为json。我猜我的java对象没有正确注释
{result:[2]
{city:[
cityId : "",
cityDesc:"",
]
area:[
areaId : "",
areaDesc:"",
]
userId : ""},
{city:[
cityId : "",
cityDesc:"",
]
area:[
areaId : "",
areaDesc:"",
]
userId : ""}}
以下是我的java对象
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "area", propOrder = {
"areaId",
"areaname"})
public class Area {
private String areaId;
private String areaDesc;
public String getAreaId() {
return areaId;
}
public void setAreaId(String areaId) {
this.areaId = areaId;
}
public String getAreaDesc() {
return areaDesc;
}
public void setAreaDesc(String areaDesc) {
this.areaDesc = areaDesc;
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "city", propOrder = {
"cityId",
"cityName"})
public class City {
private String cityId;
private String cityDesc;
public String getCityId() {
return cityId;
}
public void setCityId(String cityId) {
this.cityId = cityId;
}
public String getCityDesc() {
return cityDesc;
}
public void setCityDesc(String cityDesc) {
this.cityDesc = cityDesc;
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Response", propOrder = {
"userId",
"city",
"area"})
public class Response {
@XmlAttribute
protected String userId;
@XmlAttribute
protected City city;
@XmlAttribute
protected Area area;
@XmlRootElement(name = "Result")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Result", propOrder = {
})
public class Result {
private List<Response> result = new ArrayList<Response>();
public List<Response> getResult() {
return result;
}
public void setResult(List<Response> result) {
this.result = result;
}
我的资源方法ID如下
@autowired
private C c;
@POST
@Path("/bil")
@Produces({MediaType.APPLICATION_JSON})
@Consumes({MediaType.APPLICATION_JSON})
public Result collBil(@Valid Input input){
Result result = new Result();
String username=iInput.getUserName();
if(null != username){
try{
result=c.getId(username);
}
catch(Exception e){
e.printStackTrace();
}
}
return result;
}
使用泽西框架。能够从我的函数成功返回Result类型。但在那之后我在其余的客户端检查我的抛出500错误。我的任何方法都没有抛出异常。但是我的java对象没有转换为json。任何帮助表示赞赏
答案 0 :(得分:0)
你需要做这样的事情:
@XmlRootElement(name = "User")
public class User {
private String username;
/**
* @return the username
*/
@JsonProperty(value = "Username")
public String getUsername() {
return username;
}
/**
* @param username the username to set
*/
@XmlElement(name = "Username")
public void setUsername(String username) {
this.username = username;
}
}
并将jackson-all-1.6.1.jar
添加到您的依赖项中
并在web.xml
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>
com.sun.jersey.spi.container.servlet.ServletContainer
</servlet-class>
<init-param>
<!-- <param-name>com.sun.jersey.config.property.packages</param-name> -->
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.restful.rest</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
并在REST
班级
@POST
@Path("/post")
@Consumes({MediaType.APPLICATION_JSON , MediaType.APPLICATION_XML})
@Produces(MediaType.TEXT_XML)
public Response getUserName(User user) {
return Response.status(201).entity(user).build();
}
答案 1 :(得分:0)
您的每个类Area,City,Response和Result很可能需要一个零参数构造函数。