Spring注解@Controller和@RestController的区别?

科学上网 | 国外VPS推荐 | 机场推荐

官方文档:Annotation Type RestController 解释说:A convenience annotation that is itself annotated with @Controller and @ResponseBody

也就是@RestController注解相当于控制器前加@Controller,方法前加@ResponseBody共同的作用效果。

@RestController
@RequestMapping(value = "/login")
public class LoginController {
    @RequestMapping(value = "/loginSuccess")
    public String loginSuccess(SrClass srClass) {
        return "success";
    }
}

上面这段代码相当于

@Controller
@RequestMapping(value = "/login")
public class LoginController {

    @RequestMapping(value = "/loginSuccess")
    @ResponseBody
    public String loginSuccess(SrClass srClass) {
        return "success";
    }
}

如果第二段代码不加@ResponseBody,返回的就是success.jsp页面,加上之后返回的就是success字符串;而第一段代码返回的也是success,不再经过视图解析器解析了。

赞(1)
未经允许禁止转载:四个空格 » Spring注解@Controller和@RestController的区别?

评论 抢沙发

登录

找回密码

注册