0

I have 2 method in controller:

@RestController("/api/v1/users")
public class UserController {


    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public List<User> getUserByEmailAndPassword(@RequestParam("email") String email, @RequestParam("password") String password) {
        logger.info("Request to get user with email and password: " + email + password);
        User user = userService.checkUserByEmailAndPassword(email, password);
        return user != null ? Collections.singletonList(user) : Collections.emptyList();
    }

    @RequestMapping(method = RequestMethod.GET)
    public List<UserDto> getUserByEmail(@RequestParam("email") String email) {
        logger.info("Request to get user with email: " + email);
        UserDto userDto = userService.checkUserEmail(email);
        return userDto != null ? Collections.singletonList(userDto) : Collections.emptyList();
    }
}

CORS config:

@Configuration
public class CorsConfig {

    @Bean
    public FilterRegistrationBean corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true);
        config.addAllowedOrigin("*");
        config.addAllowedHeader("*");
        config.addAllowedMethod("*");
        source.registerCorsConfiguration("/**", config);
        FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
        bean.setOrder(0);
        return bean;
    }
}

When I send request to: http://localhost:9092/api/v1/users/login?email=art&password=1037006322

I calling this method getUserByEmail

How it is possible? How to fix this problem?

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
bsuart
  • 351
  • 1
  • 4
  • 24
  • What is the expected behaviour? Currently it works as expected due to `@RequestMapping` first being applied from class level and then from method level. – Karol Dowbecki Mar 20 '18 at 15:43
  • 1
    @KarolDowbecki Request http://localhost:9092/api/v1/users/login?email=art&password=1037006322 should call getUserByEmailAndPassword – bsuart Mar 20 '18 at 15:45

1 Answers1

1

Map this way.it will work as per your requirement.Map your url pattern with @RequestMapping instead of @RestController.

@RestController
@RequestMapping("/api/v1/users")
public class UserController {

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public List<User> getUserByEmailAndPassword(@RequestParam("email") String email, @RequestParam("password") String password) {
        logger.info("Request to get user with email and password: " + email + password);
        User user = userService.checkUserByEmailAndPassword(email, password);
        return user != null ? Collections.singletonList(user) : Collections.emptyList();
    }

    @RequestMapping(method = RequestMethod.GET)
    public List<UserDto> getUserByEmail(@RequestParam("email") String email) {
        logger.info("Request to get user with email: " + email);
        UserDto userDto = userService.checkUserEmail(email);
        return userDto != null ? Collections.singletonList(userDto) : Collections.emptyList();
    }

 }
Sanjay
  • 2,481
  • 1
  • 13
  • 28
  • It would be great if you can explain what is the difference between mapping uri to `@RestController` and `@RequestMapping`. – Nisarg Patil Mar 20 '18 at 16:39
  • Just check : https://stackoverflow.com/questions/48891083/difference-between-parameters-taken-by-restcontroller-and-requestmapping-annot – Sanjay Mar 20 '18 at 16:47