我开发了Sping Boot CRUD应用程序,连接的数据库是PostgreSQL,@GetMapping
工作正常,GET请求可以检索到一个空的对象数组,但是在@PostMapping
中,POST请求给出404错误。
📌Spring概述应用程序.java
package com.example.SpringRecap;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@SpringBootApplication(exclude = SecurityAutoConfiguration.class)
//@RequestMapping("api/v1/customers")
//@EnableWebMvc
@RequestMapping(name = "api/v1/customers" ,method = RequestMethod.POST)
public class SpringRecapApplication {
//dependency injection
private static CustomerRepository customerRepository;
public SpringRecapApplication(CustomerRepository customerRepository) {
this.customerRepository = customerRepository;
}
public static void main(String[] args) {
SpringApplication.run(SpringRecapApplication.class, args);
}
@GetMapping
public List<Customer> getCustomer() {
return customerRepository.findAll();
}
record NewCustomerRequest(
String name,
String email,
Integer age
) {
@PostMapping
public void addCustomer(@RequestBody NewCustomerRequest newCustomerRequest) {
Customer customer = new Customer();
customer.setAge(newCustomerRequest.age());
customer.setName(newCustomerRequest.name());
customer.setEmail(newCustomerRequest.email());
customerRepository.save(customer);
}
}
}
customerRepository.save(customer);
不允许使依赖注入最终化。(private static CustomerRepository customerRepository;
)。IDEA建议使其静态化。但它不起作用。当我使用@RequestMapping("api/v1/customers")
时,收到了一个405错误。然后我通过以下操作修复了这个问题,
@RequestMapping(name = "api/v1/customers" ,method = RequestMethod.POST)
📌客户存储库.java
package com.example.SpringRecap;
import org.springframework.data.jpa.repository.JpaRepository;
public interface CustomerRepository extends JpaRepository<Customer,Integer> {
}
📌客户.java
package com.example.SpringRecap;
import jakarta.persistence.*;
import java.util.Objects;
@Entity
public class Customer {
@Id
@SequenceGenerator(
name = "customer_id_sequence",
sequenceName = "customer_id_sequence",
allocationSize = 1
)
@GeneratedValue(
strategy = GenerationType.SEQUENCE,
generator = "customer_id_sequence"
)
private Integer id;
private String name;
private String email;
private Integer age;
public Customer(Integer id, String name, String email, Integer age) {
this.id = id;
this.name = name;
this.email = email;
this.age = age;
}
public Customer() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Customer customer = (Customer) o;
return Objects.equals(id, customer.id) && Objects.equals(name, customer.name) && Objects.equals(email, customer.email) && Objects.equals(age, customer.age);
}
@Override
public int hashCode() {
return Objects.hash(id, name, email, age);
}
@Override
public String toString() {
return "Customer{" +
"id=" + id +
", name='" + name + '\'' +
", email='" + email + '\'' +
", age=" + age +
'}';
}
}
Postman :
如果需要更多信息才能获得解决方案,请发表评论。
2条答案
按热度按时间06odsfpq1#
您的代码问题在于您将POST端点指定为DTO的一部分,而不是控制器的一部分。由于您的DTO不是Spring托管bean,Spring不会将URLMap到您的端点。无论如何,您应该将端点移到一个单独的类中。示例:
最好将DTO也移到一个单独的类中。我建议将DTO放在
dto
包中,将控制器放在controller
包中。两个附注:你不应该通过你的API暴露你的实体。你应该使用DTO来处理传入和传出的数据。看看lombok和mapstruct,它们让这变得非常容易。
nwwlzxa72#
试试这个
@RequestMapping("api/v1/customers")//@EnableWebMvc //@RequestMapping(name = "api/v1/customers" ,method = RequestMethod.POST)