【发布时间】:2021-06-04 15:57:29
【问题描述】:
我正在尝试使用 REST 查询过滤器接口编写 Spring Boot 应用程序。
我想要达到的目标:localhost:8080/persons/search?age=42&city=Berlin&skilltype=Sport
基本类型(String、Int、...)的正常过滤已经成功运行。我在过滤另一个表的属性时遇到问题。
@Entity
public class Person {
@Id
@Column(name = "id", nullable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "age")
private Integer age;
@Column(name = "city")
private String city;
@Column(name = "skills")
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "person_id")
private Set<Skill> skills;
@Entity
public class Skill {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "name", nullable = false)
private String name;
@Column(name = "type", nullable = false)
private String type;
}
控制器
import net.kaczmarzyk.spring.data.jpa.domain.*;
import net.kaczmarzyk.spring.data.jpa.web.annotation.And;
import net.kaczmarzyk.spring.data.jpa.web.annotation.Join;
@GetMapping(value = "/persons/search", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<List<Person>> getPersonBySpecification(
@Join(path = "skills", alias = "s")
@And({
@Spec(path = "id", params = "id", spec = Equal.class),
@Spec(path = "name", params = "name", spec = EqualIgnoreCase.class),
@Spec(path = "age", params = "age", spec = Equal.class),
@Spec(path = "city", params = "city", spec = Like.class),
@Spec(path = "zipCode", params = "zipCode", spec = In.class),
@Spec(path = "s.name", params = "skillname", spec = In.class),
@Spec(path = "s.type", params = "skilltype", spec = In.class),
})
Specification<Person> spec) {
return ResponseEntity.ok().body(this.personService.getPersonBySpecification(spec));
}
异常java.lang.NullPointerException: Cannot invoke "Object.getClass()" because "cause" is null
非常感谢您的任何建议!
【问题讨论】:
标签: java spring spring-boot hibernate jpa