@restcontroller
@restcontroller = @controller + @responsebody组成,等号右边两位同志简单介绍两句,就明白我们@restcontroller的意义了:
@controller 将当前修饰的类注入springboot ioc容器,使得从该类所在的项目跑起来的过程中,这个类就被实例化。当然也有语义化的作用,即代表该类是充当controller的作用
@responsebody 它的作用简短截说就是指该类中所有的api接口返回的数据,甭管你对应的方法返回map或是其他object,它会以json字符串的形式返回给客户端,本人尝试了一下,如果返回的是string类型,则仍然是string。
@restcontroller@requestmapping(test)public class samplecontroller {
@getmapping public map testget() {
return new hashmap<string, string>(){{
put(name, springboot);
}};
}
@getmapping(path = str)
public string testgetstr() { return ok; }}
这部分代码对于map返回则是json string,对于string则仍然是string
当将@restcontroller换成@controller之后,对于/test的返回值如下图:
从报错可以看见,当@controller修饰的时候,spring以为会返回一个view(也就是mvc中的那c)但是返回的东西却是一个map。
以上就是springboot http中@restcontroller的作用是什么的详细内容。