【发布时间】:2023-03-09 00:46:01
【问题描述】:
我正在尝试通过带有 Spring Data JPA 的 Spring Boot 2 应用程序将来自邮递员的数据发布到 MySQL 数据库中。我得到的只是 404 错误。
主要
@SpringBootApplication
public class ProfileApplication {
public static void main(String[] args) {
SpringApplication.run(ProfileApplication.class, args);
}
}
实体
@Entity
public @Data class Profile {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String profileText;
}
控制器
@RestController
@RequestMapping(value = "/profile", produces = { MediaType.APPLICATION_JSON_VALUE })
public class ProfileController {
@Autowired
private ProfileRepository profileRepository;
public ProfileRepository getRepository() {
return profileRepository;
}
@GetMapping("/profile/{id}")
Profile getProfileById(@PathVariable Long id) {
return profileRepository.findById(id).get();
}
@PostMapping("/profile")
Profile createOrSaveProfile(@RequestBody Profile newProfile) {
return profileRepository.save(newProfile);
}
}
存储库
public interface ProfileRepository extends CrudRepository<Profile, Long> {
}
application.properties
server.port = 8080
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/profiledb
spring.datasource.username=root
spring.datasource.password=
server.servlet.context-path=/service
【问题讨论】:
-
你有没有在你的机器上本地运行你的mysql服务器实例?
-
请在问题中添加邮递员的卷曲或截图。
-
@MohsenAbasi MySQL 在本地是 tunnint。
-
404 表示找不到你调用的api。也许您使用了错误的路径。
-
看看@Alexandru Somai 的回答。
标签: java spring spring-boot spring-data-jpa