【发布时间】:2021-08-27 15:41:57
【问题描述】:
所以,我试图用基本的 GET POST PUT DELETE 命令编写一个微服务。 它是一个带有 mysql 服务器的 Spring Maven Java 程序
当我尝试发布某些内容时,它可以工作,但是如果我不包含 ID,它会返回异常:
"status": 500,
"error": "Internal Server Error",
"message": "could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement",
这很奇怪,因为在 mysql 控制台中我可以插入没有 id 的客户,它只是自动增量。 null 也可以。但是,在 Id 为空的情况下发布不起作用
这是我的一些代码:
@Service
public class CustomerService {
private final CustomerRepository customerRepository;
@Autowired
public CustomerService(CustomerRepository customerRepository) {
this.customerRepository = customerRepository;
}
public List<Customer> getCostumer() {
return customerRepository.findAll();
}
public void addNewCustomer(Customer customer) {
customerRepository.save(customer);
System.out.println(customer);
}
public void deleteCustomer(Long customerId) {
if (!customerRepository.existsById(customerId)) {
throw new IllegalStateException("customer with id " +customerId+ " does not exist");
}
customerRepository.deleteById(customerId);
}
}
@RestController
@RequestMapping(path = "api/v1/customer")
public class CustomerController {
private final CustomerService customerService;
@Autowired
public CustomerController(CustomerService customerService) {
this.customerService = customerService;
}
@GetMapping
public List<Customer> getCostumer() {
return customerService.getCostumer();
}
@PostMapping
public void registerNewCustomer(@RequestBody Customer customer) {
customerService.addNewCustomer(customer);
}
@DeleteMapping(path = "{customerId}")
public void deleteCustomer(@PathVariable("customerId") Long customerId) {
customerService.deleteCustomer(customerId);
}
}
@Repository
public interface CustomerRepository extends JpaRepository<Customer, Long> {
Optional<Customer> findCustomerById(Long id);
}
@Entity
@Table
public class Customer {
@Id
private String name;
private String contactFirstName;
private String contactLastName;
private String phoneNumber;
private String fax;
private String city;
private String region;
private String country;
private String zip;
private float creditLimit;
private String adress_1;
private String adress_2;
private Long id;
public Customer() {
}
public Customer(String name, String contactFirstName, String contactLastName, String phoneNumber, String fax, String city, String region, String country, String zip, float creditLimit, String adress_1, String adress_2, Long id) {
this.name = name;
this.contactFirstName = contactFirstName;
this.contactLastName = contactLastName;
this.phoneNumber = phoneNumber;
this.fax = fax;
this.city = city;
this.region = region;
this.country = country;
this.zip = zip;
this.creditLimit = creditLimit;
this.adress_1 = adress_1;
this.adress_2 = adress_2;
this.id = id;
}
public Customer(String name, String contactFirstName, String contactLastName, String phoneNumber, String fax, String city, String region, String country, String zip, float creditLimit, String adress_1, String adress_2) {
this.name = name;
this.contactFirstName = contactFirstName;
this.contactLastName = contactLastName;
this.phoneNumber = phoneNumber;
this.fax = fax;
this.city = city;
this.region = region;
this.country = country;
this.zip = zip;
this.creditLimit = creditLimit;
this.adress_1 = adress_1;
this.adress_2 = adress_2;
}
//the rest of the code is just getters and setters
已解决 解决办法是:
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id",unique=true, nullable = false)
private Long id;
谢谢大家!
【问题讨论】:
-
签入您的
Customer模型,您将id设置为自动递增。 -
我认为主要问题是您的实体类。所以如果可能的话,你能发布你的实体定义吗?
-
请提供足够的代码,以便其他人更好地理解或重现问题。
标签: java spring http post microservices