【发布时间】:2020-09-28 07:47:48
【问题描述】:
我正在使用 QueryDsl 中的谓词。后端内部使用camelCase,但承诺在与客户端通信时使用snake_case。
我想使用snake_case 作为查询参数,比如
http://localhost:8080/inputmethod?protocol_type=SDK
如果我将protocol_type 作为snake_case 传递,Query Dsl Predicate 不会解析它。它仅将类型(目标实体类)的字段解析为 camelCase。所以protocol_type 被QuerydslPredicateBuilder 的getPredicate() 跳过。
/**
* 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
如何设置Predicate的命名约定为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
【问题讨论】:
标签: spring-data naming-conventions querydsl predicate spring-data-commons