it编程 > 编程语言 > Java

Springboot请求和响应相关注解及使用场景分析

5人参与 2025-12-09 Java

在spring boot中,请求和响应相关的注解主要用于处理http请求和构建http响应。以下是常用的请求和响应相关注解的详细介绍,包括使用场景和示例。

1. 请求处理注解

@requestmapping

@restcontroller
@requestmapping("/api")
public class mycontroller {
    @requestmapping(value = "/hello", method = requestmethod.get)
    public string sayhello() {
        return "hello, world!";
    }
}

@getmapping, @postmapping, @putmapping, @deletemapping, @patchmapping

@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

@getmapping("/greet")
public string greet(@requestparam(name = "name", defaultvalue = "world") string name) {
    return "hello, " + name;
}

@pathvariable

@getmapping("/items/{id}")
public item getitem(@pathvariable("id") long id) {
    // logic to fetch item by id
    return item;
}

@requestbody

@postmapping("/items")
public item createitem(@requestbody item item) {
    // logic to create item
    return item;
}

@requestheader

@getmapping("/user-agent")
public string getuseragent(@requestheader("user-agent") string useragent) {
    return "user-agent: " + useragent;
}

2. 响应处理注解

@responsebody

@getmapping("/items/{id}")
@responsebody
public item getitem(@pathvariable("id") long id) {
    // logic to fetch item by id
    return item;
}

@restcontroller

@restcontroller
@requestmapping("/api")
public class mycontroller {
    @getmapping("/items/{id}")
    public item getitem(@pathvariable("id") long id) {
        return item;
    }
}

@responsestatus

@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

@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请求和响应内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

您想发表意见!!点此发布评论

推荐阅读

mybatis-plus分表实现案例(附示例代码)

12-09

通过案例理解Spring中静态代理

12-09

Spring中静态代理与动态代理的实现及区别对比分析

12-09

Java调用DeepSeek API的8个高频坑与解决方法

12-09

Spring Boot 与 Spring Cloud

12-09

Java线程池配置原则与实战解析

12-09

猜你喜欢

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论