gpt4 book ai didi

java - 请求在 Controller 中获取另一个方法(Spring Boot)

转载 作者:行者123 更新时间:2023-12-02 11:29:17 28 4
gpt4 key购买 nike

我在 Controller 中有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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com