返回一个json时,你怎么能告诉Spring你想要去某个url?

时间:2017-10-16 02:57:15

标签: spring spring-mvc

在我的代码中,我返回一个String形式的路径(/ successPassword或/ systemError)。基本上这会告诉Spring转到路径/ successPassword或/ systemError,/ successPassword转到successPassword.html,/ systemError转到systemError.html。这些html文件位于src / main / resources / templates。

@RequestMapping(value = "/user/new_password", method = RequestMethod.POST)
public String createNewPassword(@RequestParam(value = "newpassword", required = false) String password,
        @RequestParam(value = "hash") String hash, Model model) {

    LOG.info("set new password using hash " + hash);
    LOG.info("new password " + password);

    boolean hashValid = passwordService.isHashValid(hash);

    if (hashValid) {
        ValidateMessage message = passwordService.validateNewPassword(password);
        if (!message.isError()) {
            Account account = passwordService.getAccount(hash);
            passwordResetService.saveNewPassword(password, account);
            model.addAttribute("account", account);
            return "/successPassword";
        } else {
            LOG.info("Password not complex enough.");
            return "/systemError";
        }
    } else {
        LOG.info("Invalid hash.");
        return "/systemError";
    }
}

现在看起来我需要返回一个json对象。我知道我必须使用@ResponseBody来注释该方法,但我的问题是我现在如何打包我将返回的对象,以便接收它的人知道它需要去哪条路径?我的html代码使用的是Thymeleaf(没有任何javascript)。

0 个答案:

没有答案