在spring boot中,请求和响应相关的注解主要用于处理http请求和构建http响应。以下是常用的请求和响应相关注解的详细介绍,包括使用场景和示例。
1. 请求处理注解
@requestmapping
- 描述: 用于映射http请求到处理方法上。
- 使用场景: 可以用于类级别和方法级别,适用于所有http方法(get, post, put, delete等)。
- 示例:
@restcontroller
@requestmapping("/api")
public class mycontroller {
@requestmapping(value = "/hello", method = requestmethod.get)
public string sayhello() {
return "hello, world!";
}
}@getmapping, @postmapping, @putmapping, @deletemapping, @patchmapping
- 描述: 这些是
@requestmapping的快捷形式,用于特定的http方法。 - 使用场景: 用于简化请求映射。
- 示例:
@restcontroller
@requestmapping("/api")
public class mycontroller {
@getmapping("/hello")
public string sayhello() {
return "hello, world!";
}
@postmapping("/create")
public string createitem(@requestbody item item) {
return "item created";
}
}@requestparam
- 描述: 用于将http请求参数绑定到方法参数上。
- 使用场景: 处理查询参数。
- 示例:
@getmapping("/greet")
public string greet(@requestparam(name = "name", defaultvalue = "world") string name) {
return "hello, " + name;
}
@pathvariable
- 描述: 用于将uri模板变量绑定到方法参数上。
- 使用场景: 处理路径参数。
- 示例:
@getmapping("/items/{id}")
public item getitem(@pathvariable("id") long id) {
// logic to fetch item by id
return item;
}@requestbody
- 描述: 用于将http请求体绑定到方法参数上,并使用httpmessageconverter进行反序列化。
- 使用场景: 处理请求体中的json或xml数据。
- 示例:
@postmapping("/items")
public item createitem(@requestbody item item) {
// logic to create item
return item;
}
@requestheader
- 描述: 将http请求头中的某个值绑定到方法参数上。
- 使用场景: 读取特定请求头信息。
- 示例:
@getmapping("/user-agent")
public string getuseragent(@requestheader("user-agent") string useragent) {
return "user-agent: " + useragent;
}
2. 响应处理注解
@responsebody
- 描述: 将方法返回值绑定到http响应体上,并使用httpmessageconverter进行序列化。
- 使用场景: 直接返回json或xml格式的数据。
- 示例:
@getmapping("/items/{id}")
@responsebody
public item getitem(@pathvariable("id") long id) {
// logic to fetch item by id
return item;
}
@restcontroller
- 描述: 组合注解,相当于@controller和@responsebody的结合,所有方法默认返回json或xml格式的数据。
- 使用场景: 用于标记restful控制器。
- 示例:
@restcontroller
@requestmapping("/api")
public class mycontroller {
@getmapping("/items/{id}")
public item getitem(@pathvariable("id") long id) {
return item;
}
}@responsestatus
- 描述: 用于设置http响应状态码。
- 使用场景: 将特定的状态码返回到客户端。
- 示例:
@postmapping("/items")
@responsestatus(httpstatus.created)
public item createitem(@requestbody item item) {
return item;
}3. 异常处理注解
@exceptionhandler
- 描述: 用于处理特定的异常,并返回自定义的响应。
- 使用场景: 处理控制器中的异常。
- 示例:
@restcontroller
@requestmapping("/api")
public class mycontroller {
@getmapping("/items/{id}")
public item getitem(@pathvariable("id") long id) {
if (id == null) {
throw new itemnotfoundexception("item not found");
}
return item;
}
@exceptionhandler(itemnotfoundexception.class)
@responsestatus(httpstatus.not_found)
public string handleitemnotfoundexception(itemnotfoundexception ex) {
return ex.getmessage();
}
}4. cors支持
@crossorigin
- 描述: 用于启用跨域资源共享(cors)。
- 使用场景: 允许跨域请求。
- 示例:
@restcontroller
@requestmapping("/api")
@crossorigin(origins = "http://example.com")
public class mycontroller {
@getmapping("/items/{id}")
public item getitem(@pathvariable("id") long id) {
return item;
}
}5. 其他相关注解
@requestattribute
- 描述: 将请求范围内的属性绑定到方法参数上。
- 使用场景: 处理在请求处理过程中设置的属性。
- 示例:
@getmapping("/attributes")
public string getattribute(@requestattribute("myattribute") string myattribute) {
return "attribute: " + myattribute;
}
@valid
- 描述: 用于验证请求体中的对象。
- 使用场景: 对输入数据进行验证。
- 示例:
@postmapping("/items")
public item createitem(@valid @requestbody item item) {
return item;
}
这些注解为spring boot提供了强大的请求和响应处理能力,帮助开发者轻松创建restful风格的api。通过合理使用这些注解,可以使代码更加清晰和易于维护。
到此这篇关于springboot请求和响应相关注解及使用场景的文章就介绍到这了,更多相关springboot请求和响应内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论