Web services 为什么CXF rest web服务在ArrayList中只有一个对象时返回对象?
当列表只有一个对象时,CXF rest web服务将返回该对象,而不是JSON数组。它适用于多个对象。你能帮我解决这个问题吗 对于列表中的两个对象,JSON响应为Web services 为什么CXF rest web服务在ArrayList中只有一个对象时返回对象?,web-services,rest,cxf,Web Services,Rest,Cxf,当列表只有一个对象时,CXF rest web服务将返回该对象,而不是JSON数组。它适用于多个对象。你能帮我解决这个问题吗 对于列表中的两个对象,JSON响应为 { "list": [ { "@xsi.type": "interaction", "interactionId": 92009, "interactionTitle": "How are you? How is going?", "interactionType": "Question" }, { "@xsi.
{
"list": [
{
"@xsi.type": "interaction",
"interactionId": 92009,
"interactionTitle": "How are you? How is going?",
"interactionType": "Question"
},
{
"@xsi.type": "interaction",
"interactionId": 92004,
"interactionTitle": "This is not working",
"interactionType": "Complaint"
}
],
"message": "Request successfully processed",
"totalRecords": 5,
"statusCode": 2000
}
{
"list": {
"@xsi.type": "interaction",
"interactionId": 92009,
"interactionTitle": "How are you? How is going?",
"interactionType": "Question"
},
"message": "Request successfully processed",
"totalRecords": 5,
"statusCode": 2000
}
对于列表中的一条记录,JSON响应为
{
"list": [
{
"@xsi.type": "interaction",
"interactionId": 92009,
"interactionTitle": "How are you? How is going?",
"interactionType": "Question"
},
{
"@xsi.type": "interaction",
"interactionId": 92004,
"interactionTitle": "This is not working",
"interactionType": "Complaint"
}
],
"message": "Request successfully processed",
"totalRecords": 5,
"statusCode": 2000
}
{
"list": {
"@xsi.type": "interaction",
"interactionId": 92009,
"interactionTitle": "How are you? How is going?",
"interactionType": "Question"
},
"message": "Request successfully processed",
"totalRecords": 5,
"statusCode": 2000
}
我希望在这两种情况下都使用JSON数组。但它返回的是JSON对象,而不是单个对象的JSON数组
这里我使用ApacheCXFREST框架公开web服务
下面是服务器端代码示例
@GET
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("/interactions")
public VerveResponse<Interaction> getUserInteractions(){
VerveResponse<Interaction> verveResponse = new VerveResponse<Interaction>();
verveResponse.setStatusCode(""+StatusCode.PROCESSED_SUCCESSFULLY);
verveResponse.setMessage("Request successfully processed");
List<Interaction> interactionList = ExternalService.getInteractionList();
verveResponse.setList(interactionList);
return verveResponse;
}
@XmlRootElement(name = "response")
@XmlSeeAlso({Interaction.class})
public class VerveResponse<T> implements Serializable{
private String statusCode;
private String message;
private List<T> list;
private Long records;
public String getStatusCode() {
return statusCode;
}
public void setStatusCode(String statusCode) {
this.statusCode = statusCode;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Long getRecords() {
return records;
}
public void setRecords(Long records) {
this.records = records;
}
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
}
@XmlRootElement(name = "interaction")
public class Interaction implements Serializable {
private Long interactionId;
private String interactionTitle;
private String interactionType;
public Long getInteractionId() {
return interactionId;
}
public void setInteractionId(Long interactionId) {
this.interactionId = interactionId;
}
public String getInteractionTitle() {
return interactionTitle;
}
public void setInteractionTitle(String interactionTitle) {
this.interactionTitle = interactionTitle;
}
public String getInteractionType() {
return interactionType;
}
public void setInteractionType(String interactionType) {
this.interactionType = interactionType;
}
}
@GET
@使用(MediaType.APPLICATION_JSON)
@产生(MediaType.APPLICATION_JSON)
@路径(“/interactions”)
public VerveResponse getUserInteractions(){
VerResponse VerResponse=新的VerResponse();
VerResponse.setStatusCode(“+StatusCode.PROCESSED_成功);
setMessage(“请求已成功处理”);
List interactionList=ExternalService.getInteractionList();
设置列表(interactionList);
返回ververresponse;
}
@XmlRootElement(name=“response”)
@XMLSEEALLO({Interaction.class})
公共类VerveResponse实现可序列化{
私有字符串状态码;
私有字符串消息;
私人名单;
私人长期记录;
公共字符串getStatusCode(){
返回状态码;
}
公共无效设置状态代码(字符串状态代码){
this.statusCode=状态码;
}
公共字符串getMessage(){
返回消息;
}
公共无效设置消息(字符串消息){
this.message=消息;
}
公众长期纪录(){
退货记录;
}
公共记录(长记录){
这个.记录=记录;
}
公共列表getList(){
退货清单;
}
公共无效集合列表(列表){
this.list=列表;
}
}
@XmlRootElement(name=“交互”)
公共类交互实现了可序列化{
私有长交互ID;
私有字符串交互标题;
私有字符串交互类型;
公共长getInteractionId(){
返回交互ID;
}
public void setInteractionId(长interactionId){
this.interactionId=interactionId;
}
公共字符串getInteractionTitle(){
返回交互标题;
}
public void setInteractionTitle(字符串interactionTitle){
this.interactionTitle=interactionTitle;
}
公共字符串getInteractionType(){
返回交互类型;
}
public void setInteractionType(字符串interactionType){
this.interactionType=interactionType;
}
}
您应该添加一些代码;否则,几乎不可能复制@MGorgon Yes。。它的副本。。。该帖子提供的解决方案对我不起作用&西科尔斯基(回答该问题的人)让我发布新问题。你可以在那个帖子里找到我的评论