问题:在Spring Boot中将JSON参数映射到类DTO的名称问题。

huangapple go评论60阅读模式
英文:

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:

{
	&quot;latitude&quot;: 52.52,
	&quot;longitude&quot;: 13.41,
	&quot;start_date&quot;: &quot;2023-05-08&quot;,
	&quot;end_date&quot;: &quot;2023-05-22&quot;
}

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(&quot;start_date&quot;)
    private String startDate;
    @JsonProperty(&quot;end_date&quot;)
    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(&quot;api/v1/demo&quot;)
public class OpenMeteoController {


    @GetMapping
    @ResponseBody
    public ResponseEntity&lt;?&gt; 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 = &quot;start_date&quot;)
	private String startDate;
	@JsonProperty(value = &quot;end_date&quot;)
	private String endDate;
    
}

huangapple
  • 本文由 发表于 2023年5月28日 18:19:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76350996.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定