0

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);
    }

}
1
  • so to be clear it fails in your getAll method? Commented Mar 3, 2020 at 20:11

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

So i deleted my answer. and voted this up. You are right about the mixing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.