Spring JPA QuerydslPredicate Snake Case

假装没事ソ 提交于 2021-02-18 18:31:32

问题


I am using spring boot with Query DSL. I have configured my spring boot to use snake case i.e. spring.jackson.property-naming-strategy=SNAKE_CASE. So my json payload input and output is in snake case like below

{
  "first_name": "First",
  "last_name": "Last"  
}

I am using @QuerydslPredicate for my search functionality. If I enter query params in snake case like below

(http://localhost:8080/context/resource/?first_name=First)

it is not parsed by Query DSL Predicate. However, If I provide the params in camel case like below

(http://localhost:8080/context/resource/?firsName=First)

it works fine. This is not synonymous with json structure.

How can I achieve snake case in query params so that Query DSL parses it?

This is how my REST end point looks like

@GetMapping
public ResponseEntity<Output> listUsers(@QuerydslPredicate(root = User.class) Predicate predicate, Pageable pageable) {
            ...
            return new ResponseEntity<>(output, HttpStatus.OK);
        }

PN: My Model Object is in camel case like below

Model Object

public class User {
    private String firstName;
    private String lastName;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
}

I came across a solution on HandlerMethodArgumentResolver, but that is lot of work. There should be a way to automate it than doing all model fields.

来源:https://stackoverflow.com/questions/53273966/spring-jpa-querydslpredicate-snake-case

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!