问题描述
我正在使用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数据映射中的属性拆分器。因此,映射器会将其转换为protocol
和type