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

渭南市住房和城乡建设局网站网上开店平台有哪些

渭南市住房和城乡建设局网站,网上开店平台有哪些,erp软件销售,全达seoResponseBody 是SpringMVC框架中的一个注解#xff0c;将方法返回值转换为HTTP响应体内容 当 ResponseBody 注解应用在一个控制器方法上时#xff0c;SpringMVC会将该方法的返回对象#xff08;如Java对象、字符串或基本类型#xff09;通过MessageConverter转换为指定的媒… ResponseBody 是SpringMVC框架中的一个注解将方法返回值转换为HTTP响应体内容 当 ResponseBody 注解应用在一个控制器方法上时SpringMVC会将该方法的返回对象如Java对象、字符串或基本类型通过MessageConverter转换为指定的媒体类型如JSON、XML等然后直接写入HTTP响应体中而不是经过视图解析器查找和渲染视图。 使用样例 RESTful API开发 RESTful API开发在创建REST服务时通常需要将业务对象以JSON或其他格式发送给客户端。这时可以使用 ResponseBody 来直接返回对象框架会自动进行序列化。 返回基本数据类型 import org.springframework.web.bind.annotation.*;RestController public class CalculatorController {GetMapping(/calculate)ResponseBodypublic double addNumbers(RequestParam(num1) double num1, RequestParam(num2) double num2) {return num1 num2;} } 返回JSON格式数据 import org.springframework.web.bind.annotation.*;RestController public class ProductController {GetMapping(/products/{id})ResponseBodypublic Product getProduct(PathVariable Long id) {// 获取产品逻辑...Product product productService.findById(id);return product; // 返回的对象会被自动序列化为JSON并发送给客户端}PostMapping(/products)ResponseBodypublic Product createProduct(RequestBody Product newProduct) {// 创建产品逻辑...Product createdProduct productService.create(newProduct);return createdProduct; // 同样将返回的对象序列化为JSON响应体} } 返回字符串 import org.springframework.web.bind.annotation.*;RestController public class MessageController {GetMapping(/message)ResponseBodypublic String getMessage() {return Hello, World!; // 直接返回一个字符串它会作为HTTP响应体内容} }// 或者更复杂的动态生成的字符串 GetMapping(/dynamic-message) ResponseBody public String getDynamicMessage(RequestParam String name) {return Hello, name !; // 根据请求参数生成消息并作为响应体返回 } 返回自定义对象集合 import org.springframework.web.bind.annotation.*;RestController RequestMapping(/api/employees) public class EmployeeController {GetMappingResponseBodypublic ListEmployee getAllEmployees() {// 获取所有员工逻辑...ListEmployee employees employeeService.getAll();return employees; // 返回的员工列表会被转换为JSON并发送给客户端} }// 假设Employee类如下 public class Employee {private Long id;private String name;private String position;// 构造函数、getter和setter省略 }返回HTTP状态码及响应体 import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*;RestController public class StatusController {GetMapping(/status/{code})ResponseBodypublic ResponseEntityString getStatus(PathVariable int code) {if (HttpStatus.resolve(code) null) {return ResponseEntity.badRequest().body(Invalid status code);}return ResponseEntity.status(code).body(Requested status code: code);} }返回分页数据 import org.springframework.data.domain.Page; import org.springframework.web.bind.annotation.*;RestController RequestMapping(/api/products) public class ProductController {private final ProductService productService;public ProductController(ProductService productService) {this.productService productService;}GetMapping(params {page, size})ResponseBodypublic PageProduct getProducts(RequestParam(page) int page, RequestParam(size) int size) {return productService.getProducts(page, size);} }返回异步处理并返回结果 import org.springframework.scheduling.annotation.Async; import org.springframework.web.bind.annotation.*; import java.util.concurrent.CompletableFuture;RestController public class AsyncController {AsyncGetMapping(/async-result)ResponseBodypublic CompletableFutureString getAsyncResult() {// 模拟耗时操作try {Thread.sleep(2000);} catch (InterruptedException e) {Thread.currentThread().interrupt();}return CompletableFuture.completedFuture(This is the result from an asynchronous operation);} }返回异步返回文件下载链接 import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.scheduling.annotation.Async; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController;RestController public class FileDownloadController {AsyncGetMapping(/generate-file)ResponseBodypublic FutureResponseEntityString generateFileAndReturnLink(RequestParam(filename) String filename) {// 模拟文件生成过程try {Thread.sleep(5000); // 模拟耗时操作} catch (InterruptedException e) {Thread.currentThread().interrupt();}// 假设fileService.generateFile(filename)返回的是文件存储路径String fileUrl fileService.generateFile(filename);HttpHeaders headers new HttpHeaders();headers.setContentType(MediaType.TEXT_PLAIN);return CompletableFuture.completedFuture(ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION, attachment; filename\ filename \).header(HttpHeaders.LOCATION, fileUrl).body(The file has been generated. Download it from: fileUrl));} }与RestController配合使用 如果一个控制器类的所有方法都需要返回响应体内容而不是视图名那么可以将整个类标记为 RestController RestController Controller ResponseBody RestController RequestMapping(/api/users) public class UserController {GetMapping(/{id})public User getUser(PathVariable Long id) {// 不需要单独标注ResponseBody因为RestController已经提供了这个功能return userService.findById(id);} }
http://www.zqtcl.cn/news/987501/

相关文章:

  • 网站开发推广方案策划书开发公司移交给物业资料说明
  • 做响应式网站的菜单中国造价网官网
  • 爱心捐赠网站怎么做中国机械网官网
  • 好的ftp网站微信小程序开发基础
  • 西安 网站 公司wordpress+帖子置顶
  • 广州开发网站服务上海千途网站建设
  • 网站建设功能分为几种百度搜索数据
  • 电影网站模板html微信开发者代码管理
  • 小程序ui界面设计手机优化大师官网
  • 佳木斯市建设局网站网络游戏名
  • 建筑钢结构网站汉阳网站建设哪家便宜
  • 营销型网站建设评价临湘网站建设
  • 做网站的价格参考巴中建网站的公司
  • 张家口建设网站网络技术工程师
  • 大型网站后台登录地址一般是如何设置的哪里网站用vue.js做的
  • 网页设计规范图标设计百度seo优化多少钱
  • 网站打开速度概念建筑网站知乎
  • 网站的flash怎么做的杭州市城乡建设网官网
  • 宿迁网站建设排名wordpress多站点可视化
  • 苏州好的做网站的公司哪家好前端如何做响应式网站
  • 广州网站建设招标推广方式有哪些渠道
  • 郑州做网站狼牙东莞建设造价信息网站
  • 网站制作代理加盟国内seo服务商
  • 如何在建设银行网站预约纪念币网络销售好不好做
  • 利用淘宝视频服务做视频网站聊城做网站价格
  • 做美容行业的网站哪个好广西互联网企业
  • 做网站平台的营业执照江镇做包子网站
  • 网站建设 摄影服务wordpress破解模板
  • 网站规划中的三种常用类型宁波海曙区建设局网站
  • dede做网站湖北网址大全