spring中@restcontroller和@controller使用及区别
@restcontroller
是 spring web 提供的一个用来开发 restful web 服务的关键注解,它是 @controller
和 @responsebody
的组合注解。通过 @restcontroller
,我们可以实现处理 http 请求并直接返回 json、xml 或其他格式的数据,而不是返回视图页面。
以下是对 @restcontroller
注解的详细解析:
1. 基本定义
@restcontroller
的作用
- 它是一个 标记类 的注解,标识该类是一个 spring mvc 控制器,且所有方法的返回值会直接作为 http 响应体(response body)。
- 可以用来简化 restful api 的开发。
注解的来源
@restcontroller
是 spring 4.0 引入的,属于 org.springframework.web.bind.annotation
包。
组合关系
它相当于 @controller
和 @responsebody
的组合:
@target(value=type) @retention(value=runtime) @documented @controller @responsebody public @interface restcontroller { }
@controller
:
- 标识当前类是一个 spring mvc 控制器,用于处理 http 请求。
@responsebody
:
- 方法的返回值不会被解析为视图名称,而是直接写入 http 响应体中。
2. 使用场景
@restcontroller
一般用于开发 restful 风格的服务接口,如前后端分离的项目中,前端通过 ajax 或其他 http 客户端调用后端接口,获取 json 或 xml 数据。
与 @controller
的区别在于:
@restcontroller
专注于返回数据(如 json、xml)。@controller
通常用于返回视图页面(如 html、jsp)。
3. 使用示例
示例 1:创建一个简单的 restful api
import org.springframework.web.bind.annotation.*; @restcontroller @requestmapping("/api/users") public class usercontroller { // get 请求,获取用户信息 @getmapping("/{id}") public string getuserbyid(@pathvariable("id") long id) { return "user id: " + id; } // post 请求,创建新用户 @postmapping public string createuser(@requestbody string user) { return "user created: " + user; } }
请求说明:
@restcontroller
标注了usercontroller
这个类是 restful 控制器。- 返回值直接写入响应体,不需要额外使用
@responsebody
。 - 示例中实现了两个接口:
- 获取用户信息(
get /api/users/{id}
)。 - 创建用户(
post /api/users
)。
- 获取用户信息(
4. 与 @controller 的对比
特性 | @restcontroller | @controller |
---|---|---|
主要用途 | 处理 restful web 服务请求,返回 json 或 xml | 返回视图页面(如 thymeleaf、jsp 等)。 |
是否需要使用 @responsebody | 不需要,默认应用于所有方法返回值。 | 需要单独为每个方法标注 @responsebody。 |
返回内容 | 数据(json、xml、文本等)。 | 视图名称(如 html 文件)。 |
使用场景 | 前后端分离的项目。 | 传统 web 应用(如返回 html 页面)。 |
示例对比:
@restcontroller
示例:
@restcontroller @requestmapping("/api") public class apicontroller { @getmapping("/greeting") public string greeting() { return "hello, world!"; } }
返回结果:hello, world!
作为纯文本响应。
@controller
示例:
@controller @requestmapping("/web") public class webcontroller { @getmapping("/greeting") public string greeting() { return "greeting"; // 返回视图名称 } }
返回结果:渲染名为 greeting.html
的视图。
5. 常用注解配合使用
5.1 配合 @requestmapping
@requestmapping
用于指定类或方法的请求路径。可以与 @restcontroller
配合使用,设置 restful api 的基础路径。
示例:
@restcontroller @requestmapping("/api") public class mycontroller { @getmapping("/hello") public string sayhello() { return "hello, api!"; } }
请求路径:get /api/hello
5.2 配合 @getmapping
、@postmapping
@getmapping
:用于处理 http get 请求。@postmapping
:用于处理 http post 请求。
示例:
@restcontroller @requestmapping("/users") public class usercontroller { // get 请求 @getmapping("/{id}") public string getuser(@pathvariable long id) { return "user id: " + id; } // post 请求 @postmapping public string createuser(@requestbody string user) { return "created user: " + user; } }
5.3 配合 @requestbody
@requestbody
将 json 请求体转换为 java 对象。
示例:
@restcontroller @requestmapping("/books") public class bookcontroller { @postmapping public string createbook(@requestbody book book) { return "created book: " + book.gettitle(); } } class book { private string title; private string author; // getter and setter }
请求数据:
{ "title": "spring in action", "author": "craig walls" }
5.4 配合 @pathvariable
和 @requestparam
@pathvariable
:从请求路径中获取变量。@requestparam
:从查询参数中获取值。
示例:
@restcontroller @requestmapping("/products") public class productcontroller { @getmapping("/{id}") public string getproduct(@pathvariable long id, @requestparam string name) { return "product id: " + id + ", name: " + name; } }
- 请求路径:
get /products/123?name=phone
- 返回结果:
product id: 123, name: phone
6. 返回 json 数据
@restcontroller
默认将返回值序列化为 json 格式(如果依赖的 jackson
库存在)。
示例:
@restcontroller @requestmapping("/users") public class usercontroller { @getmapping("/{id}") public user getuser(@pathvariable long id) { return new user(id, "john"); } } class user { private long id; private string name; // constructor, getter, setter }
返回结果:
{ "id": 1, "name": "john" }
7. 常见问题
7.1 返回值无法序列化
- 如果返回的对象没有提供
getter
或没有无参构造函数,可能导致 json 序列化失败。 - 解决方法:确保返回的对象有正确的
getter
和无参构造器。
7.2 404 not found
错误
- 检查请求路径是否正确。
- 确保控制器类和方法的
@requestmapping
路径匹配。
7.3 415 unsupported media type
问题
- 如果使用
@requestbody
,确保请求头中有content-type: application/json
。
8. 特性总结
特性 | 描述 |
---|---|
组合注解 | 是 @controller 和 @responsebody 的组合。 |
返回数据格式 | 默认返回 json 数据(需要依赖 jackson 库)。 |
适用场景 | restful api 开发,前后端分离项目接口开发。 |
简化开发 | 不需要额外为每个方法添加 @responsebody。 |
总结
@restcontroller
是 spring web 提供的一个便捷注解,专用于处理 restful api 请求。- 它简化了传统的
@controller
+@responsebody
的开发方式。 - 配合其他注解(如
@requestmapping
、@getmapping
、@requestbody
等)可以快速开发健壮的 restful 服务。 - 在前后端分离的应用中,
@restcontroller
是必不可少的工具之一。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论