[英]Spring boot restful web service. Xml response wrongly formatted
I've got a simple Restful webService using Spring Boot 2.1, Java 8, running on Eclipse Neon. 我有一个运行在Eclipse Neon上的使用Spring Boot 2.1,Java 8的简单Restful web服务。 Im sending the following request:
我发送以下请求:
<patentListWrapper>
<patentList>
<patent>
<guid>bbb</guid>
</patent>
<patent>
<guid>ccc</guid>
</patent>
</patentList>
</patentListWrapper>
and im getting back the following (incorrect) response: 并且即时通讯返回以下(错误)响应:
<patentListWrapper>
<patentList>
<patentList>
<guid>ddd</guid>
</patentList>
<patentList>
<guid>eee</guid>
</patentList>
</patentList>
</patentListWrapper>
ie i've got 2 patentList elements in the response ,instead of an inner patent element, and I don't know why. 即我在响应中有2个PatentList元素,而不是内部的Patent元素,我不知道为什么。 My 2 POJO classes to map the request are:
我的2个映射请求的POJO类是:
public class PatentListWrapper {
private List<Patent> patents;
public List<Patent> getPatentList() {
return patents;
}
public void setPatentList(List<Patent> patents) {
this.patents = patents;
}
}
and: 和:
public class Patent {
private String guid;
public String getGuid() {
return guid;
}
public void setGuid(String guid) {
this.guid = guid;
}
public Patent() {
super();
}
}
my REST Controller class is: 我的REST Controller类是:
@RestController
public class PndController {
@Autowired
ReadFromDb db;
@RequestMapping(value = "/guidRequest/xmlList", method = RequestMethod.POST, produces = { "application/xml", "text/xml" }, consumes = MediaType.ALL_VALUE )
public PatentListWrapper guidSearchList(@RequestBody PatentListWrapper patentListWrapper) {
System.out.println("DS in guidSearchList()");
patentListWrapper = db.readGuidsFromDb(patentListWrapper); // Set the guid in the patents List in patentListWrapper
return patentListWrapper;
}
}
and ReadFromDb class: 和ReadFromDb类:
@Repository
public class ReadFromDb {
public PatentListWrapper readGuidsFromDb(PatentListWrapper patentListWrapper) {
List<Patent> patents= patentListWrapper.getPatentList();
for(Patent patent : patents) {
patent.setGuid("aaa");
}
patentListWrapper.setPatentList(patents);
return patentListWrapper;
}
}
I'm sending my resuest using the windows ARC Advanced Rest Client: Rest client with Content-type=application/xml 我正在使用Windows ARC Advanced Rest客户端发送请求书:Content-type = application / xml的Rest客户端
I've established that both patentList element names map to get PatentList () in PatentListWrapper. 我已经建立了两个 patentList元素名称的映射,以获取PatentListWrapper中的PatentList ()。 How do I get the response envelope to match the request envelop?
如何获得响应信封以匹配请求信封? Any help appreciated.
任何帮助表示赞赏。
it is true , just create the getter setter method with the same variable name like below instead of using different names for getter setter methods 的确如此,只需使用如下相同的变量名创建getter setter方法,而不是对getter setter方法使用不同的名称
private List<Patent> patents;
public List<Patent> getPatents() {
return patents;
}
public void setPatents(List<Patent> patents) {
this.patents = patents;
}
or use the GSON and use @JsonProperty and define the required value name , further if you are not using the IDE to generate getters and setters you better use lombok plugin . 或使用GSON并使用@JsonProperty并定义所需的值名称,此外,如果您不使用IDE来生成getter和setter,则最好使用lombok插件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.