当前位置: 首页 > wzjs >正文

网站建设的收费标准网站多久备案一次吗

网站建设的收费标准,网站多久备案一次吗,武威做网站的公司,韩国网页游戏网站Spring MVC 处理 HTTP 状态码、响应头和异常的完整示例 1. 正常响应处理 通过 ResponseEntity 可以灵活控制 HTTP 状态码、响应头和响应体。 代码示例:创建资源返回 201 并设置 Location 头 import org.springframework.http.HttpHeaders; import org.springfram…

Spring MVC 处理 HTTP 状态码、响应头和异常的完整示例


1. 正常响应处理

通过 ResponseEntity 可以灵活控制 HTTP 状态码、响应头和响应体。

代码示例:创建资源返回 201 并设置 Location 头
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;@RestController
@RequestMapping("/api/users")
public class UserController {@PostMappingpublic ResponseEntity<User> createUser(@RequestBody User user) {// 保存用户逻辑(假设已成功保存)user.setId(1L); // 假设生成的用户ID为1// 设置 Location 头指向新资源的 URIHttpHeaders headers = new HttpHeaders();headers.setLocation(ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}").buildAndExpand(user.getId()).toUri());return new ResponseEntity<>(user, headers, HttpStatus.CREATED); // 201 Created}
}

代码示例:成功响应返回 200 并设置 Cache-Control 头
@GetMapping("/{id}")
public ResponseEntity<User> getUser(@PathVariable Long id) {User user = userService.findById(id); // 假设用户存在HttpHeaders headers = new HttpHeaders();headers.setCacheControl(CacheControl.noCache()); // 禁止缓存return new ResponseEntity<>(user, headers, HttpStatus.OK); // 200 OK
}

2. 异常处理

通过 @ResponseStatus@ControllerAdvice 实现异常状态码与响应控制。

自定义异常类(带 @ResponseStatus)
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;@ResponseStatus(code = HttpStatus.NOT_FOUND, reason = "User not found")
public class UserNotFoundException extends RuntimeException {public UserNotFoundException(String message) {super(message);}
}
全局异常处理类(@ControllerAdvice)
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;@ControllerAdvice
public class GlobalExceptionHandler {@ExceptionHandler(UserNotFoundException.class)public ResponseEntity<ErrorDetails> handleUserNotFound(UserNotFoundException ex, WebRequest request) {ErrorDetails error = new ErrorDetails(HttpStatus.NOT_FOUND.value(),ex.getMessage(),request.getDescription(false));return new ResponseEntity<>(error, HttpStatus.NOT_FOUND);}@ExceptionHandler(Exception.class)public ResponseEntity<ErrorDetails> handleGeneralException(Exception ex, WebRequest request) {ErrorDetails error = new ErrorDetails(HttpStatus.INTERNAL_SERVER_ERROR.value(),"Internal Server Error",request.getDescription(false));return new ResponseEntity<>(error, HttpStatus.INTERNAL_SERVER_ERROR);}// 辅助类:错误响应体private static class ErrorDetails {private int statusCode;private String message;private String path;public ErrorDetails(int statusCode, String message, String path) {this.statusCode = statusCode;this.message = message;this.path = path;}// 省略 getter/setter}
}

3. 响应头设置示例
@GetMapping("/custom-headers")
public ResponseEntity<String> customHeaders() {HttpHeaders headers = new HttpHeaders();headers.add("X-Custom-Header", "custom-value");headers.set("Content-Type", "text/plain");headers.set("Access-Control-Allow-Origin", "*");return new ResponseEntity<>("Response with custom headers", headers, HttpStatus.OK);
}

4. 关键注解与类说明
注解/类作用示例
@ResponseStatus在异常类上定义默认 HTTP 状态码和原因。@ResponseStatus(HttpStatus.NOT_FOUND)
ResponseEntity直接控制 HTTP 状态码、响应头和响应体。new ResponseEntity<>(data, headers, HttpStatus.OK)
@ControllerAdvice全局异常处理类,集中管理异常响应。@ControllerAdvice + @ExceptionHandler
HttpStatusHTTP 状态码枚举(如 HttpStatus.OK, HttpStatus.CREATED)。HttpStatus.NOT_FOUND

5. 场景总结表格
场景实现方式状态码响应头示例适用情况
成功创建资源ResponseEntity + HttpStatus.CREATED201Location: /api/users/1新资源创建成功后返回位置
返回成功数据ResponseEntity + HttpStatus.OK200Cache-Control: no-cache正常业务响应
资源不存在异常@ResponseStatus(HttpStatus.NOT_FOUND)404资源查询失败
全局异常处理(如服务器错误)@ControllerAdvice + @ExceptionHandler500捕获通用未处理异常
自定义响应头ResponseEntity 设置 HttpHeaders200X-Custom-Header: custom-value需要添加自定义响应头时

关键总结

  1. 状态码控制
    • ResponseEntity 直接指定状态码(如 HttpStatus.CREATED)。
    • @ResponseStatus 在异常类上定义默认状态码。
  2. 响应头管理
    • 通过 HttpHeaders 对象添加任意头信息。
  3. 异常处理
    • 自定义异常 + @ResponseStatus:针对特定异常返回状态码。
    • @ControllerAdvice:全局统一处理异常,返回结构化错误信息。
  4. 最佳实践
    • 使用 ResponseEntity 精确控制响应细节。
    • 通过 ErrorDetails 统一错误响应格式。
    • 对于常见 HTTP 状态码(如 404、500),优先使用标准枚举值。
http://www.dtcms.com/wzjs/820176.html

相关文章:

  • 几大门户网站网站建设先进个人事迹
  • 网站建设公司止一se0汝州网站建设汝州
  • WordPress网站仿制电子商务网站开发需求分析
  • 怎做网站wordpress谷歌字体去除
  • 免备案网站建设地方旅游网站模板
  • 网站开通告知书东莞网站开发公司
  • 做第一个php网站个人网页开发毕业设计
  • 海西电子商务网站建设网络营销专业建议
  • 熊猫头表情包制作网站查询行业信息的网站
  • 网站正在建设中中文模板网站后台做1个多少钱
  • 网站建设添加汉语vue大型网站开发
  • 网站表格布局网上创建公司
  • html网站登录界面模板下载免费windows云服务器
  • 做网站前台要学什么课程tomcat如何部署wordpress
  • 东莞网站优化方案在aws上安装WordPress
  • 沧州建网站酒店网站建设项目
  • 珠海营销型网站html静态页面怎么放在网站上
  • wordpress4.9 多站点新闻软文发稿平台
  • 建设商务网站的方案做艺术教育的网站
  • 太原市住房和城乡建设局网站首页北京手机网站开发
  • 多个网站对比表格怎么做宁波seo网络推广渠道介绍
  • 什么是网站程序服务平台推广
  • 织梦教育网站开发中石油企业邮箱怎么注册
  • 网站开发这行怎么样申请app一般需要多少费用
  • 深圳十大网站建设wordpress 抱歉您不能访问此页面
  • 如何自己开发一款app网站推广优化公司
  • 做谷歌网站吗物流网站建设方案权限管理
  • 微信 网站设计模板wordpress不使用缩略图
  • 泰州企业建站系统新手跨境电商怎么做
  • 网站做一个要多少钱南昌专业网站制作公司