[英]Getting 404 Error when calling a Spring Boot API
我编写了一些请求映射,并使用 JPA 将我的 Spring 引导应用程序连接到 Postgresql 数据库。 但是,当我尝试调用 API 时,我收到以下消息: {"timestamp":"2021-01-30T21:58:34.028+00:00","status":404,"error":"Not Found","message":"","path":"/api/v1/sessions"}
。 我尝试在调用 API 并且它工作时打印一条消息,所以我认为它可能与 JPA 连接有关? (我还使用 SQL Shell 测试了数据库和凭据是否良好,它们都可以)
我的 model:
@Entity(name = "sessions")
public class Session {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long session_id;
private String session_name;
private String session_description;
private Integer session_length;
@ManyToMany
@JoinTable(
name = "session_speakers",
joinColumns = @JoinColumn(name = "session_id"),
inverseJoinColumns = @JoinColumn(name = "speaker_id"))
private List<Speaker> speakers;
我的 controller:
@Controller
@RequestMapping("/api/v1/sessions")
public class SessionsController {
@Autowired
private SessionRepository sessionRepository;
@GetMapping
public List<Session> list() {
System.out.println("Get method called");
return sessionRepository.findAll();
}
@GetMapping
@RequestMapping("{id}")
public Session get(@PathVariable Long id) {
return sessionRepository.getOne(id);
}
我的存储库:
public interface SessionRepository extends JpaRepository<Session, Long> {
}
最后,我的应用程序属性:
spring.datasource.url=jdbc:postgresql://localhost:5432/databaseName
spring.datasource.username=postgres
spring.datasource.password=mypasswordhere
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.show-sql=true
@GetMapping(value = "/{id}")<---remove Request mapping and also add the slash here
public Session get(@PathVariable Long id) {
return sessionRepository.getOne(id);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.