《springboot》后端接收Content-Type :application/json类型的数据, 前端传参

Content-Type的几种类型

1.application/x-www-form-urlencoded  传key-val

application/x-www-form-urlencoded:浏览器默认请求,以键值对的形式k1=v1&k2=v2

可以使用springMVC默认的自定义参数接收,也可以通过@requestParam()注解接收,或者对象接收

2.application/json  传json

application/json:是以json字符串的格式请求

application/json:只能以@requestBody注解接收对象,但是springMVC中用@requestBody之后就不能接收单个参数了

springboot接收

	@RequestMapping(value ="/md5" ,produces = "application/json;charset=UTF-8")
	public String md5(@RequestBody ParamsBean bean) {
		
		System.out.println(bean.getAppKey());

		return "md5";
	}

3.text/xml 传 html

前端传字符串流, 传html


	    var context = "/db";
		var contentStr = "13888888888"
		console.log("---------前端传输-------------", contentStr);
	      $.ajax({
	          type: "POST",
	          url: context + "/demo/stream",
	          async: false, 
	          data: contentStr,
	          success: function(res){
	          		console.log("-----------后端返回----------", res.data);
	          }
	     });

后端接收



	@RequestMapping("/request")
	void request(HttpServletRequest request, HttpServletResponse response) {
        //这是个参数bean
        AppObtainTokenParam param =null;

		try {
			BufferedReader streamReader = new BufferedReader( new InputStreamReader(request.getInputStream(), "UTF-8"));
			StringBuilder responseStrBuilder = new StringBuilder();
			String inputStr;
			while ((inputStr = streamReader.readLine()) != null){
				responseStrBuilder.append(inputStr);
			}

			System.out.println(responseStrBuilder .toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

4. multipart/form-data 上传文件

POST 数据提交的方式。我们在使用表单上传文件时,必须让 form 的 enctyped 等于这个值。

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Content-Type: application/json是用于设置请求头的内容类型,它指示服务端请求或响应中的消息主体是序列化的JSON字符串。 在后端通常会希望获取到JSON格式的数据时,需要设置该Content-Type头部,例如:headers: { "Content-Type": "application/json" }。 通过设置Content-Type: application/json,可以方便地传递复杂多层次的对象,并将其序列化为JSON字符串。 需要注意的是,Content-Type头部不仅在请求中用于指示请求的消息主体类型,也在响应中用于指示响应的消息主体类型。当从服务端下载文件时,可以通过检查响应头中的Content-Type来确定返回的文件类型。 总结来说,Content-Type: application/json是一种常用的标识,用于指示请求或响应的消息主体是JSON格式的数据。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [content-type: application/json](https://blog.csdn.net/aa3115386/article/details/79616794)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [三种常见Content-Type格式,最后一种你肯定用过](https://blog.csdn.net/weixin_45809580/article/details/123671085)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值