本篇博客试用于编写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数据代码也就完成了,希望帮助到你。记得给赞哦!!!