Spring boot 未找到能够从类型转换为类型2的转换器
我知道这是另一个类似的问题,但我自己无法回答,这就是我写信给你寻求帮助的原因 我尝试创建自己的@Query,并在两种情况下返回转换错误。我猜这项服务有问题,但我的知识就到此为止了 这是我的密码: 主要实体Spring boot 未找到能够从类型转换为类型2的转换器,spring-boot,jpa,spring-data-jpa,Spring Boot,Jpa,Spring Data Jpa,我知道这是另一个类似的问题,但我自己无法回答,这就是我写信给你寻求帮助的原因 我尝试创建自己的@Query,并在两种情况下返回转换错误。我猜这项服务有问题,但我的知识就到此为止了 这是我的密码: 主要实体 @Data @Entity @Table(name = "users") public class User { @Id @GeneratedValue(strategy = GenerationType.AUTO) private long id; pr
@Data
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String name;
private String city;
private Date startDate;
private boolean stat;
public User() {
}
public User(String name, String city, Date startDate, boolean stat) {
this.name = name;
this.city = city;
this.startDate = startDate;
this.stat = stat;
}
}
2.第二种模式
public class UserName {
private String firstname;
public UserName() {
}
public UserName(String firstname) {
this.firstname = firstname;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
}
3.第三种模式
public class UserCount {
private String city;
private int count;
public UserCount() {
}
public UserCount(String city, int count) {
this.city = city;
this.count = count;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
存储库
@存储库
公共接口用户存储库扩展了JpaRepository{
}
服务
@服务
公共类用户服务{
@自动连线
私有用户存储库回购
public List<UserName> getN (){
return repo.getNameUsers();
}
public List<UserCount> getC(){
return repo.getOwnQuery();
}
}
控制器
@控制器
公共类MyController{
@Autowired
private UserRepository repo;
@Autowired
private UserService repoService;
@GetMapping("/") //1.it's work
ResponseEntity<List<User>> getAllCity(Pageable page){
return ResponseEntity.ok(repo.getAll());
}
@GetMapping("/s") //2.it's work
ResponseEntity<List<User>> getAllUsers(Pageable page){
return ResponseEntity.ok(repo.findByFirstnameEndsWith("Seba"));
}
@GetMapping("/f") ///3.don't work
ResponseEntity<List<UserName> >getUsersName(Pageable page){
return ResponseEntity.ok(repoService.getN());
}
@GetMapping("/c") ///4.don't work
ResponseEntity<List<UserCount> >getUsersCount(Pageable page){
return ResponseEntity.ok(repoService.getC());
}
}
它还将源代码添加到
对不起,我没有添加错误代码
在@Query中使用带有新关键字的构造函数来获取列表
并对列表执行相同的操作
加上你在完整堆栈跟踪中的错误,我不得不承认我也是新来的。非常感谢你的回答
@Autowired
private UserRepository repo;
@Autowired
private UserService repoService;
@GetMapping("/") //1.it's work
ResponseEntity<List<User>> getAllCity(Pageable page){
return ResponseEntity.ok(repo.getAll());
}
@GetMapping("/s") //2.it's work
ResponseEntity<List<User>> getAllUsers(Pageable page){
return ResponseEntity.ok(repo.findByFirstnameEndsWith("Seba"));
}
@GetMapping("/f") ///3.don't work
ResponseEntity<List<UserName> >getUsersName(Pageable page){
return ResponseEntity.ok(repoService.getN());
}
@GetMapping("/c") ///4.don't work
ResponseEntity<List<UserCount> >getUsersCount(Pageable page){
return ResponseEntity.ok(repoService.getC());
}
@Query("select NEW com.sub.model.UserName(u.name) from User u ")
List<UserName> getNameUsers();
@Query("select NEW com.sub.model.UserCount(u.city, count(u)) from User u where u.stat = true group by u.city")
List<UserCount> getOwnQuery();