温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Java怎么实现后台发送及接收json数据

发布时间:2021-04-15 11:54:05 来源:亿速云 阅读:1028 作者:小新 栏目:编程语言

这篇文章主要介绍Java怎么实现后台发送及接收json数据,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

具体的内容如下:

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

package com.utils;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import net.sf.json.JSONObject;
public class testOne {
    public static void main(String[] args) {
        JSONObject jsobj1 = new JSONObject();
        JSONObject jsobj2 = new JSONObject();
        jsobj2.put("deviceID", "112");
        jsobj2.put("channel", "channel");
        jsobj2.put("state", "0");
        jsobj1.put("item", jsobj2);
        jsobj1.put("requestCommand", "control");
        post(jsobj1,"http://192.168.3.4:8080/HSDC/test/authentication");
    }
    public static String post(JSONObject json,String path) {
        String result="";
    try {
        HttpClient client=new DefaultHttpClient();
            HttpPost post=new HttpPost(url);
            post.setHeader("Content-Type", "appliction/json");
            post.addHeader("Authorization", "Basic YWRtaW46");
            StringEntity s=new StringEntity(json.toString(), "utf-8");
            s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "appliction/json"));
            post.setEntity(s);
            HttpResponse httpResponse=client.execute(post);
            InputStream in=httpResponse.getEntity().getContent();
            BufferedReader br=new BufferedReader(new InputStreamReader(in, "utf-8"));
            StringBuilder strber=new StringBuilder();
            String line=null;
            while ((line=br.readLine())!=null) {
                strber.append(line+"\n");
            }
            in.close();
            result=strber.toString();
            if(httpResponse.getStatusLine().getStatusCode()!=HttpStatus.SC_OK){
                result="服务器异常";
            }
    } catch (Exception e) {
      System.out.println("请求异常");
      throw new RuntimeException(e);
    }
    System.out.println("result=="+result);
    return result;
  }
}

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;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
@RestController
@RequestMapping("test")
public class TestConttroller{
  @Resource
    protected HttpServletRequest request;
    @RequestMapping(value="authentication",produces = MediaType.APPLICATION_JSON_VALUE,method = RequestMethod.POST)
    public Map<String,Object> 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<String,Object> jsonObject = new HashMap<String, Object>(); //创建Json对象
        jsonObject.put("username", "张三");     //设置Json对象的属性
        jsonObject.put("password", "123456");
        return jsonObject;
    }
}

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

Java怎么实现后台发送及接收json数据

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

Java怎么实现后台发送及接收json数据

以上是“Java怎么实现后台发送及接收json数据”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI