我如何接收一个 dto,将地图作为其属性之一,作为我的控制器的参数? (休息 API)
How do I receive a dto, with a map as one of it's atributes, as a parameter of my controller? (REST API)
当我尝试发送它时,它显示“错误”:“错误请求”,
“trace”:“...HttpMessageNotReadableException:JSON 解析错误:无法从字符串“quoteDate”反序列化 java.time.LocalDate
类型的映射键:无法反序列化 java.time.LocalDate”
我通过邮递员发送的JSON:
{
"stockId":"test3",
"quotes":[
{
"quoteDate":"2003-05-14",
"quoteValue":"35.9"
},
{
"quoteDate":"2016-03-28",
"quoteValue":"55.0"
}
]
}
控制器:
@PostMapping("/stock/save")
public void saveQuotes(@RequestBody StockDTO stockDTO) {
System.out.println(stockDTO.toString());
}
DTO
public class StockDTO {
String id;
String stockId;
Map<LocalDate, Double> quotes;
}
对于引号,根据 json 字符串应该出现的数据类型是列表。但在 DTO 中得到了一个地图。
DTO 应该是 ::
import com.google.gson.Gson;
import java.util.List;
public class GsonMappingExample {
public static void main(String[] args) {
String jsonString = "{\"stockId\":\"test3\",\"quotes\":[{\"quoteDate\":\"2003-05-14\",\"quoteValue\":\"35.9\"},{\"quoteDate\":\"2016-03-28\",\"quoteValue\":\"55.0\"}]}";
Gson gson = new Gson();
StockDTO stockDTO = gson.fromJson(jsonString, StockDTO.class);
System.out.println(stockDTO);
}
}
class StockDTO {
private final String stockId;
private final List<Quote> quotes;
public StockDTO(String stockId, List<Quote> quotes) {
this.stockId = stockId;
this.quotes = quotes;
}
@Override
public String toString() {
return "StockDTO{" +
"stockId='" + stockId + '\'' +
", quoteList=" + quotes +
'}';
}
}
class Quote {
private final String quoteDate;
private final Double quoteValue;
public Quote(String quoteDate, Double quoteValue) {
this.quoteDate = quoteDate;
this.quoteValue = quoteValue;
}
@Override
public String toString() {
return "Quote{" +
"quoteDate=" + quoteDate +
", quoteValue=" + quoteValue +
'}';
}
}
PS:这里我使用 Gson 库来解析 json 字符串,spring-boot 自动执行此操作(我认为使用 Jackson 库!)
当我尝试发送它时,它显示“错误”:“错误请求”,
“trace”:“...HttpMessageNotReadableException:JSON 解析错误:无法从字符串“quoteDate”反序列化 java.time.LocalDate
类型的映射键:无法反序列化 java.time.LocalDate”
我通过邮递员发送的JSON:
{
"stockId":"test3",
"quotes":[
{
"quoteDate":"2003-05-14",
"quoteValue":"35.9"
},
{
"quoteDate":"2016-03-28",
"quoteValue":"55.0"
}
]
}
控制器:
@PostMapping("/stock/save")
public void saveQuotes(@RequestBody StockDTO stockDTO) {
System.out.println(stockDTO.toString());
}
DTO
public class StockDTO {
String id;
String stockId;
Map<LocalDate, Double> quotes;
}
对于引号,根据 json 字符串应该出现的数据类型是列表。但在 DTO 中得到了一个地图。
DTO 应该是 ::
import com.google.gson.Gson;
import java.util.List;
public class GsonMappingExample {
public static void main(String[] args) {
String jsonString = "{\"stockId\":\"test3\",\"quotes\":[{\"quoteDate\":\"2003-05-14\",\"quoteValue\":\"35.9\"},{\"quoteDate\":\"2016-03-28\",\"quoteValue\":\"55.0\"}]}";
Gson gson = new Gson();
StockDTO stockDTO = gson.fromJson(jsonString, StockDTO.class);
System.out.println(stockDTO);
}
}
class StockDTO {
private final String stockId;
private final List<Quote> quotes;
public StockDTO(String stockId, List<Quote> quotes) {
this.stockId = stockId;
this.quotes = quotes;
}
@Override
public String toString() {
return "StockDTO{" +
"stockId='" + stockId + '\'' +
", quoteList=" + quotes +
'}';
}
}
class Quote {
private final String quoteDate;
private final Double quoteValue;
public Quote(String quoteDate, Double quoteValue) {
this.quoteDate = quoteDate;
this.quoteValue = quoteValue;
}
@Override
public String toString() {
return "Quote{" +
"quoteDate=" + quoteDate +
", quoteValue=" + quoteValue +
'}';
}
}
PS:这里我使用 Gson 库来解析 json 字符串,spring-boot 自动执行此操作(我认为使用 Jackson 库!)