0

I use snake_case DB columns and camelCase DTO. And our team want to use snake_case when we code React component.
Because of it, I added @JsonNaming on DTO. But it works when I send Json data, as you know. Is there any annotation or setting similar to @JsonNaming?

Here is my postman data and sample codes.
Debug data: sampleName=name, sampleDesc=null. enter image description here

// Controller

@RestController
@RequestMapping("/sample")
public class SampleController {

    @Autowired
    private SampleService sampleService;

    @GetMapping
    public Result getSampleList(SampleDTO param) throws Exception {
        return sampleService.getFolderList(param);
    }

    @PostMapping
    public Result insertSample(@RequestBody SampleDTO param) throws Exception {
        // this method works well with @JsonNaming
        return sampleService.insertFolder(param);
    }
}

// DTO

@Setter
@Getter
@NoArgsConstructor
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
@Alias("SampleDTO")
public class SampleDTO {

    @NotNull
    private Long sampleNo;

    @NotBlank
    private String sampleName;

    private String sampleDesc;

    @Builder
    public SampleDTO(Long sampleNo, String sampleName, String sampleDesc) {
        this.sampleNo = sampleNo;
        this.sampleName = sampleName;
        this.sampleDesc = sampleDesc;
    }

}
loveloper.dev
  • 299
  • 1
  • 4
  • 9
  • if you want name each field use @JsonProperrty("name") – sjy Aug 02 '22 at 05:27
  • @sjy I think... @ JsonProperty doesn't work with query string. I should call GET method with query string like my sample postman. Does it work with your code ?? – loveloper.dev Aug 02 '22 at 05:38
  • i thought its post , will you able to generate sampleDTO from Map parameters = request.getParameterMap(); – sjy Aug 02 '22 at 05:50

1 Answers1

0

I had the same problem and didn't find an annotation for this but maybe you can use @ConstructorProperties like this in your DTO's constructor:

@ConstructorProperties({"sample_no","sample_name","sample_desc"})
public SampleDTO(Long sampleNo, String sampleName, String sampleDesc) {
    this.sampleNo = sampleNo;
    this.sampleName = sampleName;
    this.sampleDesc = sampleDesc;
}
El Captus
  • 1
  • 2