Spring POST 方法微服务500异常
Spring POST Method Microservice 500 exception
所以,我正在尝试使用基本的 GET POST PUT DELETE 命令编写微服务。
它是一个带有 mysql 服务器
的 Spring Maven Java 程序
当我尝试 post 时它起作用了,但是如果我不包含它的 ID returns 一个例外:
"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;
谢谢大家!
你的模型指出 name
是主键,但你的数据库模型似乎有 id
主键,可能 auto_increment
分配给了它。
您应该相应地调整您的模型,例如:
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
我不确定 MySQL 的策略是正确的,但 this 可能会有所帮助。
如果您对 name
列的唯一性有限制,您可以向其添加相应的注释,如下所示:
@Column(unique = true)
private String name;
所以,我正在尝试使用基本的 GET POST PUT DELETE 命令编写微服务。 它是一个带有 mysql 服务器
的 Spring Maven Java 程序当我尝试 post 时它起作用了,但是如果我不包含它的 ID returns 一个例外:
"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;
谢谢大家!
你的模型指出 name
是主键,但你的数据库模型似乎有 id
主键,可能 auto_increment
分配给了它。
您应该相应地调整您的模型,例如:
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
我不确定 MySQL 的策略是正确的,但 this 可能会有所帮助。
如果您对 name
列的唯一性有限制,您可以向其添加相应的注释,如下所示:
@Column(unique = true)
private String name;