简体   繁体   English

Spring Boot 无法识别 MongoDB RestController 类

[英]Spring Boot does not recognize MongoDB RestController class

I am new to MongoDB and I am trying to use it with my SpringBoot application.我是 MongoDB 的新手,我正在尝试将它与我的 SpringBoot 应用程序一起使用。 I have followed my tutorials online and have downloaded their code and got it execute.我已经在线学习了我的教程并下载了他们的代码并执行了它。

However for whatever reason my project fails to be able to print out RequestMappingHandlerMapping : Mapped “{[/findAllBooks/{id}],methods=[GET]}”但是,无论出于何种原因,我的项目都无法打印 RequestMappingHandlerMapping : Mapped “{[/findAllBooks/{id}],methods=[GET]}”

I was wondering if anyone would be able to advise me if it is due to the nature of my project structure .我想知道是否有人能够建议我这是由于我的项目结构的性质。 I wasn't sure if my SpringBootMain could see my Controller class.我不确定我的 SpringBootMain 是否可以看到我的 Controller 类。

My project structure is best viewed here https://github.com/emuldrew855/backend/tree/A/B-Testing/src/main/java/com/ebay/queens/demo我的项目结构最好在这里查看https://github.com/emuldrew855/backend/tree/A/B-Testing/src/main/java/com/ebay/queens/demo

My Controller class我的控制器类

package com.ebay.queens.demo.resource;

@RestController
@RequestMapping("/v2")
public class UserController {

    @Autowired
    private UserRepository userRepository;

    @PostMapping("/AddUser")
    public String saveUser(@RequestBody User user) {
        userRepository.save(user);
        return "Added user with id: " + user.getId();
    }

    @GetMapping("/all")
    public List<User> getAll(){
        List<User> users = this.userRepository.findAll();
        return users;
    }
}

My main class我的主课

package com.ebay.queens.demo;
    @SpringBootConfiguration
    @SpringBootApplication
    public class SpringBootMain implements CommandLineRunner {
        @Autowired
        private TokenUtilityClass tokenUtilityClass;

    @Bean ResourceConfig resourceConfig() {
    return new ResourceConfig().registerClasses(Version1Api.class, Login.class, SignUp.class, Paypal.class); }

    @Override
    public void run(String... args) throws Exception {
        // test.authenticationToken();
    }

    public static void main(String[] args) {
        SpringApplication.run(SpringBootMain.class, args);
    }

}

I've figured out why is not working... You are using 2 different WebService API which are incompatible...我已经弄清楚为什么不起作用...您正在使用 2 个不同的 WebService API,它们不兼容...

Spring-Boot has native API to work with API Rest with @RestController annotation. Spring-Boot 具有原生 API,可与带有@RestController注释的 API Rest 一起@RestController You don't need to use Glassfish server.您不需要使用Glassfish服务器。

Solution #1解决方案#1

  1. From SpringBootMain remove @Bean ResourceConfig resourceConfig() {...} .SpringBootMain删除@Bean ResourceConfig resourceConfig() {...} Now, you API /v2 will work as expected.现在,您的 API /v2将按预期工作。

  2. Your API /v1 won't work because it uses other library.您的 API /v1将无法使用,因为它使用其他库。 You need to change @Path to @GetMapping or @PostMapping and add @RestController into your Version1Api class.您需要将@Path更改为@GetMapping@PostMapping并将@RestController添加到您的Version1Api类中。

Solution #2解决方案#2

You ignore Spring-Boot native Rest API and implement Glassfish Server.您忽略 Spring-Boot 原生 Rest API 并实现Glassfish Server。

  1. Add UserController.class reference添加UserController.class参考

@Bean ResourceConfig resourceConfig() { return new ResourceConfig().registerClasses(Version1Api.class, Login.class, SignUp.class, Paypal.class, UserController.class ); @Bean ResourceConfig resourceConfig() { return new ResourceConfig().registerClasses(Version1Api.class, Login.class, SignUp.class, Paypal.class, UserController.class ); } }

  1. For UserController change @RestController to @Path("/v2")对于UserController@RestController更改为@Path("/v2")

@Path("/v2")
public class UserController {

    @Autowired
    private UserRepository userRepository;

    @POST
    @Path("/AddUser")
    @Produces(MediaType.TEXT_PLAIN)
    public String saveUser(@RequestBody User user) {
        userRepository.save(user);
        return "Added user with id: " + user.getId();
    }

    @GET
    @Path("/all")
    @Produces(MediaType.APPLICATION_JSON)
    public List<User> getAll(){
        List<User> users = this.userRepository.findAll();
        return users;
    }
}

Now both API will work as expected现在两个 API 都将按预期工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM