java在调用Spring引导API时遇到404错误
我编写了一些请求映射,并使用JPA将我的Spring Boot应用程序连接到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测试了数据库和凭据是否良好,它们是否正常)
我的模型:
@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
@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
# 1 楼答案