当前位置: 首页 > 知识库问答 >
问题:

Spring Boot Postgres JPA,不返回主键以响应findAll()

江展
2023-03-14

员工班

@Entity
@Table(name = "employees")
public class Employee {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "eid", unique = true, nullable = false)
    private long eid;
    
    @Column(name ="first_name")
    private String firstName;
    
    @Column(name ="last_name")
    private String lastName;

    @Column(name ="email_id")
    private String emailId;
        
    
    public Employee() {
        
    }
    
    public Employee(String firstName, String lastName, String emailId) {
        super();
        this.firstName = firstName;
        this.lastName = lastName;
        this.emailId = emailId;
    }
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public String getEmailId() {
        return emailId;
    }
    public void setEmailId(String emailId) {
        this.emailId = emailId;
    }   
}

员工回购

@Repository
public interface EmployeeRepo extends JpaRepository<Employee, Long>{
}

控制器类

@CrossOrigin(origins = {"http://localhost:3000/"})
@RestController
@RequestMapping("/api/v1/")
public class EmployeeController {

    @Autowired  //Creates object internally on runtime
    private EmployeeRepo employeeRepo;
    
    //get all employee
    @GetMapping("/employees")
    public List<Employee> getAllEmployees(){
        return employeeRepo.findAll();      
    }
}

应用属性

Springjpa。数据库平台=组织。冬眠地方话PostgreSql方言

Springjpa。显示sql=true

spring.jpa.properties.hibernate.format_sql=true

spring.jpa.hibernate.ddl自动更新

当我向/api/v1/employees发送get请求时,我得到的响应是

[{“firstName”:“aa”,“lastName”:“bb”,“emailId”:”tom@gmail.com" } ]

我希望是[{“eid”:1,“firstName”:“aa”,“lastName”:“bb”,“emailId”:”tom@gmail.com" } ]

共有1个答案

郏博瀚
2023-03-14

我相信你需要一个员工类eid的getter。

将该方法添加到Employee类。

public long getEid() {
   return this.eid;
}
 类似资料:
  • 使用Guzzle,我正在使用JSON格式的一些外部API,通常我获取数据 $data = $request- 但是我无法从这个不同的api中获取数据。数据似乎没有出现在“响应体”中。 这个api调用有效:https://i.ibb.co/80Yk6dx/Screenshot-2.png 这不起作用:https://i.ibb.co/C239ghy/Screenshot-3.png

  • 本文向大家介绍symfony2 返回404响应,包括了symfony2 返回404响应的使用技巧和注意事项,需要的朋友参考一下 示例 当在服务器上找不到资源时,将返回404响应。在Symfony中,可以通过引发NotFoundHttpException异常来创建此状态。为了避免use在控制器内部添加多余的语句,请使用类createNotFoundException()提供的Controller  

  • 我想使用GooglePlaceAPI和自动完成视图。我有开发者控制台上的android和浏览器密钥。但当我尝试这些键API时,它返回空响应。 Logcat: D/Volley:[2651]a.a:请求的HTTP响应= 我的代码: 在onpostexecute日志中: 我是新来的google地方api,请帮我找到什么是错误的。

  • 有很多使用OkHTTP3发出请求的例子,但是,我仍然无法发出正确的请求。据我所知,我的设置是正确的。 我不断收到如下所示的回复。我不知道他们是什么意思。 {“ContentLength”:238,“ContentTypeString”:“Application/JSON”,“Source”:{“Buffer”:{“Size”:0},“Closed”:False,“Source”:{“BytesRe

  • 我正在尝试对远程服务器进行api调用,最初,我遇到以下错误:

  • 当我执行这个查询时,我会得到以下异常