Spring QuerydslPredicate蛇案的命名约定 控制器实体配置

问题描述

我正在使用QueryDsl中的谓词。后端内部使用camelCase,但承诺与客户端通信时将使用snake_case。

我想使用snake_case作为查询参数,例如

http://localhost:8080/inputmethod?protocol_type=SDK

如果我将protocol_type作为snake_case传递,则Query Dsl Predicate不会解析它。它仅将类型(目标实体类)的字段解析为camelCase。因此protocol_type的getPredicate()会跳过QuerydslPredicateBuilder

    /**
     * Creates a Querydsl {@link Predicate} for the given values,{@link QuerydslBindings} on the given
     * {@link Typeinformation}.
     *
     * @param type the type to create a predicate for.
     * @param values the values to bind.
     * @param bindings the {@link QuerydslBindings} for the predicate.
     * @return
     */
    @Nullable
    public Predicate getPredicate(Typeinformation<?> type,MultiValueMap<String,String> values,QuerydslBindings bindings) {
    ...
            if (!bindings.isPathAvailable(path,type)) { // <-- here!
            // path : "protocol_type"
            // type : my.domain.entity.InputMethod
                continue;
            }
   ...
}
   

但它可以在cameCase中使用。

http://localhost:8080/inputmethod?protocolType=SDK

如何将谓词的命名约定设置为snake_case


控制器

    @GetMapping
    public List<InputMethodDto.Response> getInputMethodTypeList(
            @QuerydslPredicate(root = InputMethod.class) Predicate predicate) {
        return service.getInputMethodList(predicate);
    }

实体


@Getter
@NoArgsConstructor
@AllArgsConstructor
@Entity
public class InputMethod {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Enumerated(EnumType.STRING)
    @Column
    private recordtype recordtype;

    @Enumerated(EnumType.STRING)
    @Column
    private ProtocolType protocolType;

    @Column
    private String name;

配置


@EnableWebMvc
@EnableSwagger2
@Configuration
public class SwaggerConfig implements WebMvcConfigurer {
    ...
}


杰克逊命名策略 我也遇到同样的问题。

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

我还设置了spring.jackson.property-naming-strategy=SNAKE_CASE

解决方法

不幸的是,恐怕您不能在这里使用snake_case,仅因为_被视为spring数据映射中的属性拆分器。因此,映射器会将其转换为protocoltype

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...