如何 return 对象列表作为 XML 在 Spring boot rest API

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

我正在编写一个 Java 网络服务,它将 return 以下 XML 格式的产品列表。

    <?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>

我正在使用如下所示的基于 class 的投影从 jdbc/hibernate 连接中获取 Name、PatchUid、RegId 标签。

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;
    }

}

现在,对于 return XML 响应,我使用下面的 DTO class 和 rest 控制器方法

    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;
    }

但是returned XML格式如下所示。请注意需要删除的额外产品标签。

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

如有任何关于从网络服务更正 returned XML 格式的建议,请告诉我。

不是从您的服务中 returning Products,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;
}

通过使用 @JacksonXmlElementWrapper(useWrapping = false) 如下更改产品 DTO class 有效。

public class 产品实现 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;
    }

}