Java實(shí)現(xiàn)后臺(tái)發(fā)送及接收json數(shù)據(jù)的方法示例
本文實(shí)例講述了Java實(shí)現(xiàn)后臺(tái)發(fā)送及接收json數(shù)據(jù)的方法。分享給大家供大家參考,具體如下:
本篇博客試用于編寫java后臺(tái)接口以及兩個(gè)項(xiàng)目之間的接口對(duì)接功能;
具體的內(nèi)容如下:
1.java后臺(tái)給指定接口發(fā)送json數(shù)據(jù)
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="服務(wù)器異常";
}
} catch (Exception e) {
System.out.println("請(qǐng)求異常");
throw new RuntimeException(e);
}
System.out.println("result=="+result);
return result;
}
}
2.java后臺(tái)接收json數(shù)據(jù)
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("進(jìn)入=====================");
//后臺(tái)接收
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);
}
//響應(yīng)
Map<String,Object> jsonObject = new HashMap<String, Object>(); //創(chuàng)建Json對(duì)象
jsonObject.put("username", "張三"); //設(shè)置Json對(duì)象的屬性
jsonObject.put("password", "123456");
return jsonObject;
}
}
運(yùn)行testOne之后將json數(shù)據(jù)發(fā)送到authentication接口,接收的數(shù)據(jù)如圖:

testOne中main方法返回的數(shù)據(jù)如圖:

至此java后臺(tái)發(fā)送及接收json數(shù)據(jù)代碼也就完成了
PS:關(guān)于json操作,這里再為大家推薦幾款比較實(shí)用的json在線工具供大家參考使用:
在線JSON代碼檢驗(yàn)、檢驗(yàn)、美化、格式化工具:
http://tools.jb51.net/code/json
JSON在線格式化工具:
http://tools.jb51.net/code/jsonformat
在線XML/JSON互相轉(zhuǎn)換工具:
http://tools.jb51.net/code/xmljson
json代碼在線格式化/美化/壓縮/編輯/轉(zhuǎn)換工具:
http://tools.jb51.net/code/jsoncodeformat
在線json壓縮/轉(zhuǎn)義工具:
http://tools.jb51.net/code/json_yasuo_trans
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java操作json格式數(shù)據(jù)技巧總結(jié)》、《Java數(shù)組操作技巧總結(jié)》、《Java字符與字符串操作技巧總結(jié)》、《Java數(shù)學(xué)運(yùn)算技巧總結(jié)》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》及《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
相關(guān)文章
Springboot使用@Cacheable注解實(shí)現(xiàn)數(shù)據(jù)緩存
本文介紹如何在Springboot中通過@Cacheable注解實(shí)現(xiàn)數(shù)據(jù)緩存,在每次調(diào)用添加了@Cacheable注解的方法時(shí),Spring 會(huì)檢查指定參數(shù)的指定目標(biāo)方法是否已經(jīng)被調(diào)用過,文中有詳細(xì)的代碼示例,需要的朋友可以參考下2023-10-10
springboot啟動(dòng)后和停止前執(zhí)行方法示例詳解
這篇文章主要介紹了springboot啟動(dòng)后和停止前執(zhí)行方法,本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08
SpringBoot日程管理Quartz與定時(shí)任務(wù)Task實(shí)現(xiàn)詳解
定時(shí)任務(wù)是企業(yè)級(jí)開發(fā)中必不可少的組成部分,諸如長(zhǎng)周期業(yè)務(wù)數(shù)據(jù)的計(jì)算,例如年度報(bào)表,諸如系統(tǒng)臟數(shù)據(jù)的處理,再比如系統(tǒng)性能監(jiān)控報(bào)告,還有搶購類活動(dòng)的商品上架,這些都離不開定時(shí)任務(wù)。本節(jié)將介紹兩種不同的定時(shí)任務(wù)技術(shù)2022-09-09
spring data jpa開啟批量插入、批量更新的問題解析
這篇文章主要介紹了spring data jpa開啟批量插入、批量更新問題,本文通過圖文實(shí)例相結(jié)合給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-07-07

