简体   繁体   中英

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. 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. My 2 POJO classes to map the request are:

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:

@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:

@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

I've established that both patentList element names map to get PatentList () in PatentListWrapper. 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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM