Java 使用Spring数据JPA在Spring Boot 2应用程序中发布数据

Java 使用Spring数据JPA在Spring Boot 2应用程序中发布数据,java,spring,spring-boot,spring-data-jpa,Java,Spring,Spring Boot,Spring Data Jpa,我正试图通过SpringBoot2应用程序将postman的数据和SpringDataJPA一起发布到MySQL数据库中。我得到的只是一个404错误 主要 实体 @Entity public @Data class Profile { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String profileText; } 控制器 @RestContro

我正试图通过SpringBoot2应用程序将postman的数据和SpringDataJPA一起发布到MySQL数据库中。我得到的只是一个404错误

主要

实体

@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> {

}
似乎在ProfileController中,您已经定义了两次概要文件端点,第一次是在类级别,第二次是在方法级别。解决方案是删除其中一个:

@RestController
@RequestMapping(value = "/profile", produces = { MediaType.APPLICATION_JSON_VALUE })
public class ProfileController {

    @Autowired
    private ProfileRepository profileRepository;

    public ProfileRepository getRepository() {
        return profileRepository;
    }

    // Notice that I've removed the 'profile' from here. It's enough to have it at class level
    @GetMapping("/{id}")
    Profile getProfileById(@PathVariable Long id) {
        return profileRepository.findById(id).get();
    }

    // Notice that I've removed the 'profile' from here. It's enough to have it at class level
    @PostMapping
    Profile createOrSaveProfile(@RequestBody Profile newProfile) {
        return profileRepository.save(newProfile);
    }
}
似乎在ProfileController中,您已经定义了两次概要文件端点,第一次是在类级别,第二次是在方法级别。解决方案是删除其中一个:

@RestController
@RequestMapping(value = "/profile", produces = { MediaType.APPLICATION_JSON_VALUE })
public class ProfileController {

    @Autowired
    private ProfileRepository profileRepository;

    public ProfileRepository getRepository() {
        return profileRepository;
    }

    // Notice that I've removed the 'profile' from here. It's enough to have it at class level
    @GetMapping("/{id}")
    Profile getProfileById(@PathVariable Long id) {
        return profileRepository.findById(id).get();
    }

    // Notice that I've removed the 'profile' from here. It's enough to have it at class level
    @PostMapping
    Profile createOrSaveProfile(@RequestBody Profile newProfile) {
        return profileRepository.save(newProfile);
    }
}

哪个网址?有效的url必须如下所示:

获取:


帖子:

哪个url?有效的url必须如下所示:

获取:


POST:

您是否在您的计算机上本地运行mysql服务器实例?请在问题中添加postman的卷曲或屏幕截图。@MohsenAbasi mysql在本地运行。404表示找不到您正在调用的api。也许您使用了错误的路径。请查看@Alexandru Somai answer。您是否在您的计算机上本地运行了mysql服务器实例?请在问题中添加curl或postman的屏幕截图。@MohsenAbasi mysql是Tunnit Local.404表示找不到您正在调用的api。也许你使用了错误的路径。看看@Alexandru Somai answer。如果他想调用/service/profile/profile,即使谈论api设计很糟糕,也不是错误。你是对的。由于他没有提到他提出的请求,我猜想他只想打电话给/service/profile。我会更新我的答案,包括我实现了你编辑的代码@AlexandruSomai。如果他想调用/service/profile/profile,即使谈论api设计很糟糕,这也不是一个错误。你是对的。由于他没有提到他提出的请求,我猜想他只想打电话给/service/profile。我会更新我的答案,包括我实现了你编辑的代码@AlexandruSomai。现在它可以工作了。
@RestController
@RequestMapping(value = "/profile", produces = { MediaType.APPLICATION_JSON_VALUE })
public class ProfileController {

    @Autowired
    private ProfileRepository profileRepository;

    public ProfileRepository getRepository() {
        return profileRepository;
    }

    // Notice that I've removed the 'profile' from here. It's enough to have it at class level
    @GetMapping("/{id}")
    Profile getProfileById(@PathVariable Long id) {
        return profileRepository.findById(id).get();
    }

    // Notice that I've removed the 'profile' from here. It's enough to have it at class level
    @PostMapping
    Profile createOrSaveProfile(@RequestBody Profile newProfile) {
        return profileRepository.save(newProfile);
    }
}