2017-05-05 25 views
0

我正在構建一個LinkedHashMap並將鍵值對放入其中。如何在返回散列圖時設置xml的父元素

如下

return new ResponseEntity(LinkedHashMapObject, httpStatus.OK) 

我返回此HashMap我得到以下輸出

<LinkedHashMap> 
    <flightnumber>001</flightnumber> 
    <Price>450</Price> 
</LinkedHashMap> 

我要的是

<flight> 
    <flightnumber>001</flightnumber> 
    <Price>450</Price> 
</flight> 

有沒有實現這一目標。

回答

1

我會建議使用描述你想要的結構的POJO。事情是這樣的:

public class Flight { 

    private Integer flightNumber; 
    private Double price; 

    public Flight() { 
    } 

    public Integer getFlightNumber() { 
     return flightNumber; 
    } 

    public void setFlightNumber(Integer flightNumber) { 
     this.flightNumber = flightNumber; 
    } 

    public Double getPrice() { 
     return price; 
    } 

    public void setPrice(Double price) { 
     this.price = price; 
    } 
} 

然後在您的迴應:

... 
Flight flight = new Flight() 
flight.setFlightNumber(123); 
flight.setPrice(450,99); 
return new ResponseEntity(flight, httpStatus.OK); 
相關問題