简体   繁体   English

请求在控制器中获取另一个方法(SpringBoot)

[英]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: 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;
    }
}

When I send request to: http://localhost:9092/api/v1/users/login?email=art&password=1037006322当我发送请求到: http://localhost:9092/api/v1/users/login?email=art&password=1037006322

I calling this method getUserByEmail我调用这个方法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.以这种方式映射。它将根据您的要求工作。使用@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();
    }

 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Controller方法中的Springboot访问标头 - Springboot access header in Controller method SpringBoot Controller 映射到不正确的方法 - SpringBoot Controller mapping to incorrect method 在 SpringBoot 中无法达到 controller 方法 - cannot reach controller method in SpringBoot 不支持Springboot请求方法&#39;POST&#39; - Springboot Request method 'POST' not supported Springboot:控制器中的@RequestParam读取带有&#39;&&#39;和&#39;#&#39;为空的请求 - Springboot : @RequestParam in controller reads the request with '&' and '#' as empty Springboot - Httponly cookie 将请求传递给 controller - Springboot - Httponly cookie Pass request to controller SpringBoot说:“不支持请求方法&#39;POST&#39; - SpringBoot says "Request method 'POST' not supported 想要在 SpringBoot 中到达 controller 之前更改请求的值 - want to change value of request before reaching controller in SpringBoot 如何使用路径和请求参数在springboot中创建/调用rest controller - How to create/call a rest controller in springboot with both path and request parameter 在 Application 上带有 RequestMapping 的 SpringBoot 和 controller 类中的另一个 RequestMapping,这可能吗? - SpringBoot with RequestMapping on Application and another RequestMapping in controller classes, is that possible?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM