我在Java中有一个DTO类型的类,它映射JSON类型的输入参数.这适用于DTO中与JSON中的字段名称匹配的字段名称.但不同的名称约定(Java名称约定和JSON名称约定),数据的类型为空(即startDate).我try 了网上建议的各种解决方案,比如使用@JsonProperty注释,但都没有效果.
我的JSON输入:
{
"latitude": 52.52,
"longitude": 13.41,
"start_date": "2023-05-08",
"end_date": "2023-05-22"
}
我的DTO课程:
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.*;
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@Builder
public class InputParametersDTO {
private float latitude;
private float longitude;
@JsonProperty("start_date")
private String startDate;
@JsonProperty("end_date")
private String endDate;
}
我的控制器:
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import java.time.LocalDateTime;
@RestController
@RequestMapping("api/v1/demo")
public class OpenMeteoController {
@GetMapping
@ResponseBody
public ResponseEntity<?> getData(InputParametersDTO inputParametersDTO) throws Exception
{
float lat = inputParametersDTO.getLatitude(); // 52.52
String endDate = inputParametersDTO.getEndDate(); // null
// DO OTHER STUFF
}
}