Joh*_*ick 1 java rest spring-boot
我是 Spring Boot 的新手。我有一个带有数据的 MYSQL 表“客户”,如下所示: 表中的数据使用 Postman 测试 API 输出时,似乎有几行空的 JSON 输出。
下面是我的代码:
package com.semika.customer;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="customer")
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name="first_name")
private String firstName;
@Column(name="last_name")
private String lastName;
@Column(name="address")
private String address;
public Customer() {
super();
}
}
Run Code Online (Sandbox Code Playgroud)
客户资料库
package com.semika.customer;
import org.springframework.data.repository.CrudRepository;
public interface CustomerRepository extends CrudRepository<Customer, Long>{
}
Run Code Online (Sandbox Code Playgroud)
客户服务
package com.semika.customer;
public interface CustomerService {
public Iterable<Customer> findAll();
}
Run Code Online (Sandbox Code Playgroud)
客户服务接口
package com.semika.customer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class CustomerServiceImpl implements CustomerService{
@Autowired
private CustomerRepository customerRepository;
public Iterable<Customer> findAll() {
return customerRepository.findAll();
}
}
Run Code Online (Sandbox Code Playgroud)
客户控制器
package com.semika.customer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class CustomerController {
@Autowired
private CustomerService customerService;
@RequestMapping("/customers")
@ResponseBody
public Iterable<Customer> findAll() {
Iterable<Customer> customers = customerService.findAll();
return customers;
}
}
Run Code Online (Sandbox Code Playgroud)
我不知道我还需要在控制器中修改什么才能看到带有数据的输出。
乍一看,您的代码看起来不错。所以,我复制了你的代码并尝试运行它,但得到了一个和你一样的空响应。花了一些时间后,我找到了原因。
Use getter and setter in you customer class and recompile the code.
Run Code Online (Sandbox Code Playgroud)
它会解决你的问题。还要进行以下更改:
1) Annotate CustomerRepository with @Repository
2) use @EnableJpaRepositories("package path") in your application's main class if your repository is not in the same or sub package.
3) use method type or @GetMapping annotation in your controller.
Run Code Online (Sandbox Code Playgroud)
为了您的方便,我在所有修改后编写您的代码:
测试演示应用程序
package testdemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@SpringBootApplication
@EnableJpaRepositories("put repository path here")
public class TestDemoApplication {
public static void main(String[] args) {
SpringApplication.run(TestDemoApplication.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
CustomerServiceImpl.java
package testdemo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class CustomerServiceImpl implements CustomerService{
@Autowired
private CustomerRepository customerRepository;
public Iterable<Customer> findAll() {
return customerRepository.findAll();
}
}
Run Code Online (Sandbox Code Playgroud)
客户服务.java
package testdemo;
public interface CustomerService {
public Iterable<Customer> findAll();
}
Run Code Online (Sandbox Code Playgroud)
客户资源库
package testdemo;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface CustomerRepository extends CrudRepository<Customer, Long>{
}
Run Code Online (Sandbox Code Playgroud)
客户控制器.java
package testdemo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class CustomerController {
@Autowired
private CustomerService customerService;
@GetMapping(value = "/customers")
public Iterable<Customer> findAll() {
Iterable<Customer> customers = customerService.findAll();
return customers;
}
}
Run Code Online (Sandbox Code Playgroud)
客户.java
package testdemo;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="customer")
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name="first_name")
private String firstName;
@Column(name="last_name")
private String lastName;
@Column(name="address")
private String address;
public Customer() {
super();
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
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 getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
Run Code Online (Sandbox Code Playgroud)
此外,CrudRepository 在 findAll() 中返回一个 Iterable<>。返回 List<> 的是 JpaRepository 所以不用担心。
| 归档时间: |
|
| 查看次数: |
1161 次 |
| 最近记录: |