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

Java 微服务当中POST form 、url、json的区别

在Java微服务的Controller中,你可以处理来自客户端的不同类型的POST请求,包括POST form、POST URL参数和POST JSON数据。以下是它们的区别以及在微服务Controller中的示例说明:

POST Form 表单数据:

当客户端以表单方式提交数据时,你的Controller可以使用@RequestParam注解来接收数据。数据被编码为key-value对,可以处理较为简单的数据交互,例如用户登录。

示例:

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;@RestController
public class FormController {@PostMapping("/submit-form")public String submitForm(@RequestParam String username, @RequestParam String password) {// 处理表单数据// ...return "Form data submitted successfully";}
}


POST URL 参数:

前端传来一个请求url,需要配合HttpServletRequest request以及@RequestParam("参数名")使用。这种方式更适合传递较多的数据,而不暴露在URL中。

示例:

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;@RestController
public class URLParamController {@PostMapping("/submit-url-param")public String submitURLParam(HttpServletRequest request, @RequestParam("macId") String macId) {// 处理传递的数据对象// ...
log.info("[ENTER getTemplate {}] macId:{}", request.getRequestURI(), macId);return "URL parameter data submitted successfully";}
}


POST JSON 数据:

当需要传递复杂结构化数据,如JSON格式的数据,可以使用@RequestBody注解接收数据。数据被编码为JSON,可以适用于更灵活的数据交换,如RESTful API。

示例:

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;@RestController
public class JSONController {@PostMapping("/submit-json")public String submitJSON(@RequestBody UserData userData) {// 处理JSON数据// ...return "JSON data submitted successfully";}
}
public class UserData {private String username;private String password;// getters and setters
}


 

http://www.lryc.cn/news/141989.html

相关文章:

  • repo 常用命令汇总——202308
  • [Linux]命令行参数和进程优先级
  • Android13新特性之通知权限提升
  • 206. 反转链表 (简单系列)
  • 攻防世界-Fakebook
  • 0基础入门C++之类和对象下篇
  • ECMAScript 2023
  • 爬虫实战之使用 Python 的 Scrapy 库开发网络爬虫详解
  • 【面试题】UDP和TCP有啥区别?
  • 字节实习后端面试总结(C++/GO)
  • linux 自动登录SSH
  • 量化:pandas基础
  • 华为云渲染实践
  • SpringBoot注解详解:从核心到Web,从数据到测试,一网打尽
  • Java寻找奇数
  • WinPlan经营大脑:精准预测,科学决策,助力企业赢得未来
  • 多数据源切换以及事务处理
  • docker 重装提示 Exising installation is up to date 解决方法
  • k8s分散部署节点之pod反亲和性(podAntiAffinity)
  • 大A的造血与吸血能力
  • 【数据库】使用ShardingSphere+Mybatis-Plus实现读写分离
  • 【第三方接口】阿里云内容审核SDK的使用
  • IDEA软件安装包分享(附安装教程)
  • 尚硅谷宋红康MySQL笔记 10-13
  • 【ag-grid-vue】基本使用
  • 学习JAVA打卡第四十四天
  • Excel通用表头及单元格合并
  • 微信小程序 自定义全局事件监听实现
  • NC65 树表型参照 搜索全部 按钮点击事件后获取sql的方法
  • 在SpringBoot使用MongoDB时出现的bug和解决