GET method: How to convert snake_case query string to camelCase DTO

1.4k views Asked by At

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;
    }

}
1

There are 1 answers

0
El Captus On

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;
}