Java实现后台发送及接收json数据的方法示例

时间:2022-09-06 23:17:09

本文实例讲述了java实现后台发送及接收json数据的方法。分享给大家供大家参考,具体如下:

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

具体的内容如下:

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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数据

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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数据代码也就完成了

ps:关于json操作,这里再为大家推荐几款比较实用的json在线工具供大家参考使用:

在线json代码检验、检验、美化、格式化工具:https://tool.zzvips.com/t/jsonjiexi/

json在线格式化工具:https://tool.zzvips.com/t/jsonformat/

在线excel转json工具:https://tool.zzvips.com/t/excel2json/

json转excel工具:https://tool.zzvips.com/t/json2excel/

在线json压缩/转义工具:https://tool.zzvips.com/t/jsonzip/

希望本文所述对大家java程序设计有所帮助。

原文链接:https://blog.csdn.net/huxiangen/article/details/80433320