Spring Boot GET请求返回200但响应体为空{},数据库有有效数据
在Spring Boot开发中,调用/getclient(按ID查询)和/getclientbyname(按名称查询)两个GET接口时,请求均返回200 OK状态码,但Postman响应体始终为{}。MySQL数据库中已存在对应查询条件的有效数据,相关代码及请求响应详情如下:
实体类(Client.java)
@Entity @Table(name = "client") public class Client { @Id // @GeneratedValue(strategy = GenerationType.IDENTITY) int clientId; @Column String clientName; @Column String clientEmail; @Column String clientPhone; public Client(){ super(); } public Client(int client_id,String client_name,String client_email,String client_phone){ super(); this.clientId = client_id; this.clientName = client_name; this.clientEmail = client_email; this.clientPhone = client_phone; } @Override public String toString() { return "Client{" + "client_id=" + clientId + ", client_name='" + clientName + '\'' + ", client_email='" + clientEmail + '\'' + ", client_phone='" + clientPhone + '\'' + '}'; } }
控制器(ClientController.java)
@RestController public class ClientController { @Autowired ClientService cserv; @GetMapping("/getclient") public ResponseEntity<Client> getClient(@RequestParam("client_id") int client_id) { return ResponseEntity.ofNullable(cserv.getClient(client_id)); } @GetMapping("/getclientbyname/{client_name}") public ResponseEntity<Client> getClientByName(@PathVariable("client_name") String client_name) { return ResponseEntity.ofNullable(cserv.getClientByName(client_name)); } }
服务类(ClientService.java)
@Service public class ClientService { @Autowired ClientRepository crepo; public Client getClient(int client_id) { Client c; Optional<Client> optionalClient = crepo.findById(client_id); try{ c = optionalClient.get(); } catch(Exception e) { c=null; } return c; } public Client getClientByName(String client_name) { Client c; Optional<Client> cl = crepo.getClientByName(client_name); try{ c = cl.get(); } catch(Exception e) { c=null; } return c; } }
仓库接口(ClientRepository.java)
@Repository public interface ClientRepository extends JpaRepository<Client,Integer> { public Optional<Client> findById(int client_id); @Query("select c from Client c where clientName=:client_name") public Optional<Client> getClientByName(String client_name); }
Postman请求响应详情
GET http://localhost:8080/getclientbyname/Client%201
200
122 ms请求头
User-Agent: PostmanRuntime/7.35.0
Accept: /
Postman-Token: 6186ba22-b040-4bcb-9da9-4932c662d41f
Host: localhost:8080
Accept-Encoding: gzip, deflate, br
Connection: keep-alive响应头
Content-Type: application/json
Transfer-Encoding: chunked
Date: Mon, 04 Dec 2023 18:31:17 GMT
Keep-Alive: timeout=60
Connection: keep-alive响应体
{}
出现问题的接口:
http://localhost:8080/getclient?client_id=1http://localhost:8080/getclientbyname/mahendra
问题根源与解决方案
1. 实体类字段无序列化权限
当前Client类的所有字段都是默认包访问权限,Spring Boot依赖的Jackson序列化框架无法访问非public字段,导致序列化后返回空JSON对象。
解决: 为字段添加private修饰符并提供对应的getter方法(符合JavaBean规范):
@Entity @Table(name = "client") public class Client { @Id // @GeneratedValue(strategy = GenerationType.IDENTITY) private int clientId; @Column private String clientName; @Column private String clientEmail; @Column private String clientPhone; public Client(){ super(); } public Client(int clientId, String clientName, String clientEmail, String clientPhone){ super(); this.clientId = clientId; this.clientName = clientName; this.clientEmail = clientEmail; this.clientPhone = clientPhone; } // 添加getter方法 public int getClientId() { return clientId; } public String getClientName() { return clientName; } public String getClientEmail() { return clientEmail; } public String getClientPhone() { return clientPhone; } @Override public String toString() { return "Client{" + "clientId=" + clientId + ", clientName='" + clientName + '\'' + ", clientEmail='" + clientEmail + '\'' + ", clientPhone='" + clientPhone + '\'' + '}'; } }
2. 仓库接口冗余方法与参数绑定问题
JpaRepository已自带findById(Integer id)方法,无需重复定义;同时JPQL查询需添加@Param注解确保参数绑定正确:
@Repository public interface ClientRepository extends JpaRepository<Client,Integer> { @Query("select c from Client c where clientName=:client_name") Optional<Client> getClientByName(@Param("client_name") String client_name); }
3. 服务类代码简化
用Optional的orElse(null)替代try-catch逻辑,代码更简洁:
@Service public class ClientService { @Autowired ClientRepository crepo; public Client getClient(int client_id) { return crepo.findById(client_id).orElse(null); } public Client getClientByName(String client_name) { return crepo.getClientByName(client_name).orElse(null); } }
内容的提问来源于stack exchange,提问作者mahendra kumar

