简体   繁体   中英

How to return a list of objects as XML in Spring boot rest API

I'm writing a Java webservice which will return the list of Products in the below XML format.

    <?xml version="1.0"?>
    
    <Products>
    <Product>
        <Name>Notepad++ 6</Name>
        <PatchUid>00038b09-0000-0000-0000-000000000000</PatchUid>
        <RegID>0</RegID>
    </Product>
    <Product>
        <Name>UltraVNC 1.2.4.0 x86</Name>   
        <PatchUid>00035767-0000-0000-0000-000000000000</PatchUid>
        <RegID>0</RegID>
    </Product>
    <Product>
        <Name>FileZilla 3</Name>    
        <PatchUid>00038c69-0000-0000-0000-000000000000</PatchUid>
        <RegID>1033</RegID>
    </Product>
    </Products>

I'm fetching the Name, PatchUid, RegId tags from the jdbc/hibernate connection using a class based projection shown below.

public class ProductView implements Serializable {

    private static final long serialVersionUID = 1L;

    @JsonProperty("Name")
    String name;
    @JsonProperty("PatchUid")
    String patchuid;
    @JsonProperty("RegId")
    String language;

    public ProductView() {
        super();
        // TODO Auto-generated constructor stub
    }

    public ProductView(String name, String patchuid, String language) {
        this.name = name;
        this.patchuid = patchuid;
        this.language = language;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPatchuid() {
        return patchuid;
    }

    public void setPatchuid(String patchuid) {
        this.patchuid = patchuid;
    }

    public String getLanguage() {
        return language;
    }

    public void setLanguage(String language) {
        this.language = language;
    }

}

Now, to return the XML response, I am using the below DTO class and rest controller method

    public class Products implements Serializable {
        
        private static final long serialVersionUID = 3639898896768313168L;
        
        
        private List<ProductView> products;
    
        public Products() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        public Products(List<ProductView> products) {
            this.products = products;
        }
    
        public List<ProductView> getProduct() {
            return products;
        }
    
        public void setProduct(List<ProductView> products) {
    
            this.products = products;
        }
    
    }
    
    
    @GetMapping("/patchMetadata")
    public Products getProductMetadata(@RequestParam(required=false) Boolean filtered) {
            
            logger.info("getProductMetadata called");
    
            List<ProductView> prodViews = null;
            
            if(filtered == null || filtered == false)
            {
                prodViews = prodMgmtService.findAllPatches();
            }
            else if (filtered == true) {
                prodViews = prodMgmtService.findFilteredPatches();
            } 
    
            Products products = new Products();
            products.setProduct(prodViews);
    
            return products;
    }

But the returned XML format is shown below. Please note the extra product tag that needs to be removed.

    <Products>
        <product>
            <product>
                <Name>FileZilla 3</Name>
                <PatchUid>0003931b-0000-0000-0000-000000000000</PatchUid>
                <RegId>0</RegId>
            </product>
            <product>
                <Name>Notepad++ 6</Name>
                <PatchUid>00038b09-0000-0000-0000-000000000000</PatchUid>
                <RegId>0</RegId>
            </product>
            <product>
                <Name>UltraVNC 1.2.4.0 x86</Name>
                <PatchUid>00035767-0000-0000-0000-000000000000</PatchUid>
                <RegId>0</RegId>
            </product>
        </product>
    </Products>

Please let me know for any suggestions to correct the returned XML format from the webservice.

Instead of returning Products from your service, return List<ProductView> :

@GetMapping("/patchMetadata")
public List<ProductView> getProductMetadata(@RequestParam(required=false) Boolean filtered) {
    List<ProductView> prodViews = null;
    if(filtered == null || filtered == false) {
        prodViews = prodMgmtService.findAllPatches();
    } else if (filtered == true) {
        prodViews = prodMgmtService.findFilteredPatches();
    } 

    return prodViews;
}

Changing the Products DTO class as below by using the @JacksonXmlElementWrapper(useWrapping = false) worked.

public class Products implements Serializable {

    private static final long serialVersionUID = 3639898896768313168L;
    
    @JacksonXmlElementWrapper(useWrapping = false)
    @JsonProperty("Product")
    private List<ProductView> product;

    public Products() {
        super();
        // TODO Auto-generated constructor stub
    }

    public Products(List<ProductView> products) {
        this.product = products;
    }

    public List<ProductView> getProduct() {
        return product;
    }

    public void setProduct(List<ProductView> products) {

        this.product = products;
    }

}

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