我在控制器中有2种方法:

@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配置:

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


当我将请求发送到:http://localhost:9092/api/v1/users/login?email=art&password=1037006322

我称这种方法为getUserByEmail

怎么可能?
如何解决这个问题?

最佳答案

按照这种方式进行映射。它将根据您的要求进行工作。使用@RequestMapping而不是@RestController映射您的url模式。

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

 }

关于java - 请求在 Controller 中获取另一种方法(Spring Boot),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49388485/

10-12 05:18