简体   繁体   中英

Request gets another method in controller(SpringBoot)

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?

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

 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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