【问题标题】:Spring POST Method Microservice 500 exceptionSpring POST 方法微服务 500 异常
【发布时间】: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


【解决方案1】:

您的模型指出 name 是主键,但您的数据库模型似乎将 id 用作主键,并可能分配了 auto_increment

您应该相应地调整您的模型,例如:

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

我不确定 MySQL 的正确策略是什么,但 this 可能会有所帮助。

如果您对 name 列有唯一性约束,您可以为其添加相应的注释,如下所示:

@Column(unique = true)
private String name;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-16
    • 2020-02-14
    • 2019-02-11
    • 2022-01-08
    • 2020-05-06
    • 2019-09-06
    • 1970-01-01
    • 2021-10-20
    相关资源
    最近更新 更多