Spring - 重定向请求处理程序

时间:2016-07-31 21:50:26

标签: java spring rest redirect

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

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

这是正确的决定吗?

1 个答案:

答案 0 :(得分:-1)

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

}

你可以尝试这种方式,就这样,你可以回复用户到你的页面。