英文:
name problem mapping JSON parameters to class DTO in spring boot
问题
我在JAVA中有一个DTO类型的类,用于映射JSON类型的输入参数。这适用于DTO中字段名称与JSON中字段名称匹配的情况(例如,latitude),但对于不匹配的字段(Java命名约定和JSON命名约定不同),数据类型为null(例如,startDate)。我尝试过各种在线建议的解决方案,比如使用@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;
}
我的Controller:
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
// 进行其他操作
}
}
英文:
I have a class in JAVA of type DTO that maps an input paramter of type JSON. This works for field names in the DTO that match those in the JSON (es. latitude) but different ones (java name convention and json name convention) the data is of type null (i.e. startDate). I have tried various soliations, suggested online, such as using the @JsonProperty annotation but to no avail.
My JSON input:
{
"latitude": 52.52,
"longitude": 13.41,
"start_date": "2023-05-08",
"end_date": "2023-05-22"
}
My DTO class:
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;
}
My Controller:
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
}
}
答案1
得分: 1
尝试以下代码,适用于我。
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.*;
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@Builder
public class InputParametersDTO {
private float latitude;
private float longitude;
@JsonProperty(value = "start_date")
private String startDate;
@JsonProperty(value = "end_date")
private String endDate;
}
英文:
try below, working for me.
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.*;
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@Builder
public class InputParametersDTO {
private float latitude;
private float longitude;
@JsonProperty(value = "start_date")
private String startDate;
@JsonProperty(value = "end_date")
private String endDate;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论