gpt4 book ai didi

java - Spring - 重定向请求处理程序

转载 作者:行者123 更新时间:2023-12-01 06:09:12 25 4
gpt4 key购买 nike

如何重定向请求处理程序?这就是我想要的:

@Controller
public class UserController {

@RequestMapping(value = "/me", method = RequestMethod.GET)
public ResponseEntity<User> getUser() {
return getUser(100500);
}

@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public ResponseEntity<User> getUser(@PathVariable("id") long id) {
User user = userService.findById(id);

if(user == null) {
return new ResponseEntity<User>(HttpStatus.NOT_FOUND);
}

return new ResponseEntity<User>(user, HttpStatus.OK);
}
}

这是正确的决定吗?

最佳答案

    @Controller
public class UserController {

@RequestMapping(value = "/me", method = RequestMethod.GET)
@ResponseBody
public User getUser() {
return getUser(100500);
}

@RequestMapping(value = "/{id}", method = RequestMethod.GET)
@Responsebody
public User getUser(@PathVariable("id") long id) {
User user = userService.findById(id);

if(user == null) {
return new ResponseEntit`enter code here`y<User>(HttpStatus.NOT_FOUND);
}

return new ResponseEntity<User>(user, HttpStatus.OK);
}

}

你可以尝试这种方式,或者那样,你可以响应用户到你的页面。

关于java - Spring - 重定向请求处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38688685/

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