1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > java 后台接受json参数的几种方式_java后台发送及接收json数据

java 后台接受json参数的几种方式_java后台发送及接收json数据

时间:2021-09-06 06:20:03

相关推荐

java 后台接受json参数的几种方式_java后台发送及接收json数据

本篇博客试用于编写java后台接口以及两个项目之间的接口对接功能;

具体的内容如下:

1.java后台给指定接口发送json数据

2.java后台接收json数据package com.controller; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.util.HashMap; import java.util.Map; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("test") public class TestConttroller extends BaseController{ @RequestMapping(value="authentication",produces = MediaType.APPLICATION_JSON_VALUE,method = RequestMethod.POST) public Map getString() throws UnsupportedEncodingException, IOException{ System.out.println("进入====================="); //后台接收 InputStreamReader reader=new InputStreamReader(request.getInputStream(),"UTF-8"); char [] buff=new char[1024]; int length=0; while((length=reader.read(buff))!=-1){ String x=new String(buff,0,length); System.out.println(x); } //响应 Map jsonObject = new HashMap(); //创建Json对象 jsonObject.put("username", "张三"); //设置Json对象的属性 jsonObject.put("password", "123456"); return jsonObject; } }

运行testOne之后将json数据发送到authentication接口,接收的数据如图:

testOne中main方法返回的数据如图:

至此java后台发送及接收json数据代码也就完成了,希望帮助到你。记得给赞哦!!!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。