Edward Muldrew
Edward Muldrew

Reputation: 273

Spring Boot does not recognize MongoDB RestController class

I am new to MongoDB and I am trying to use it with my SpringBoot application. 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]}”

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.

My project structure is best viewed here 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);
    }

}

Upvotes: 0

Views: 136

Answers (1)

Valijon
Valijon

Reputation: 13113

I've figured out why is not working... You are using 2 different WebService API which are incompatible...

Spring-Boot has native API to work with API Rest with @RestController annotation. You don't need to use Glassfish server.

Solution #1

  1. From SpringBootMain remove @Bean ResourceConfig resourceConfig() {...}. Now, you API /v2 will work as expected.

  2. Your API /v1 won't work because it uses other library. You need to change @Path to @GetMapping or @PostMapping and add @RestController into your Version1Api class.

Solution #2

You ignore Spring-Boot native Rest API and implement Glassfish Server.

  1. Add UserController.class reference

@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")

@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

Upvotes: 1

Related Questions