我正在学习java的spring Boot 这是一个通用的模式。现在我发现了一个错误,我一直在寻找,但不能找到问题的症结。
当我在postman中执行GET时,它显示错误500:
“不是实体:类com.示例.服务器端.模型.基础实体.基础实体;嵌套的异常是java.lang.IllegalArgumentException:非实体:类com.示例.服务器端.模型.基础实体.基础实体”,
我已经检查了几次,也看过类似的问题,但找不到答案。
这是我在BaseEntity、GenericRepository、区域服务和区域控制器上的代码。
模型基本实体'
package com.example.serverside.models.baseEntity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
@MappedSuperclass
public class BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
}
模型区域
package com.example.serverside.models;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import com.example.serverside.models.baseEntity.BaseEntity;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name="region")
public class Region extends BaseEntity {
@Column( nullable = false)
private String name;
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
@OneToMany(mappedBy = "region", cascade = CascadeType.ALL)
private List<Country> countries;
}
通用存储库
package com.example.serverside.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.example.serverside.models.baseEntity.BaseEntity;
@Repository
public interface GenericRepository<T extends BaseEntity> extends JpaRepository<T,Long> {
}
区域服务
package com.example.serverside.services;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.serverside.models.Region;
import com.example.serverside.repository.GenericRepository;
@Service
public class RegionService {
private GenericRepository<Region> regionRepository;
@Autowired
public RegionService(GenericRepository<Region> regionRepository) {
this.regionRepository = regionRepository;
}
// findAll
public List<Region> findAll(){
return regionRepository.findAll();
}
}
区域控制器
package com.example.serverside.controllers;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.serverside.models.Region;
import com.example.serverside.services.RegionService;
import lombok.AllArgsConstructor;
@RestController
@RequestMapping("/api/v1/region")
public class RegionController{
private RegionService regionService;
@Autowired
public RegionController(RegionService regionService) {
this.regionService = regionService;
}
@GetMapping
public ResponseEntity<List<Region>> findAll() {
return new ResponseEntity(regionService.findAll(), HttpStatus.OK);
}
}
`
我希望找到我问题的症结所在,以及如何解决它
1条答案
按热度按时间scyqe7ek1#
创建一个通用存储库的目的是什么?2BaseEntity只是一个模型,它是由真实的的实体(如Region)扩展的。3请注意它没有@Entity注解。
因此,如果要在数据库中存储区域,请为区域定义存储库。