cotroller接口层
package com.xpu.knime.controller;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.json.JSONException;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.util.List;
@RequestMapping("/getRecommendation")
@RestController
@Api("解析json")
public class JsonController {
@PostMapping("getFrequencies")
@ResponseBody
//@ApiOperation("12323")
public String getFrequencies(String nodeName) {
if (nodeName!=null){
return nodeName;
}else{
return "false";
}
}
@PostMapping("frequencies")
@ApiOperation("获得节点的概率")
@ResponseBody
public List<String> test(@RequestBody(required = false) String name) throws IOException, JSONException {
return jsonDecoder.getFrequencies(name);
}
}
请求post接口
package com.xpu.knime.controller;
import org.apache.commons.io.IOUtils;
import sun.net.www.protocol.http.HttpURLConnection;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.Arrays;
import java.util.List;
public class postJson{
public static String httpPost(String urlStr, String name){
name = "Test";
URL connect;
StringBuffer data = new StringBuffer();
try {
connect = new URL(urlStr);
HttpURLConnection connection = (HttpURLConnection)connect.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);//post不能使用缓存
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
OutputStreamWriter paramout = new OutputStreamWriter(
connection.getOutputStream(),"UTF-8");
String paramsStr = ""; //拼接Post 请求的参数
BufferedReader reader = new BufferedReader(new InputStreamReader(
connection.getInputStream(), "UTF-8"));
String line;
while ((line = reader.readLine()) != null) {
data.append(line);
}
paramout.close();
reader.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return data.toString();
//return null;
}
/**
* 发送Http post请求
*
* @param xmlInfo
* json转化成的字符串
* @param URL
* 请求url
* @return 返回信息
*/
public static String doHttpPost(String xmlInfo, String URL) throws IOException {
//System.out.println("发起的数据:" + xmlInfo);
byte[] xmlData = xmlInfo.getBytes();
InputStream instr = null;
java.io.ByteArrayOutputStream out = null;
String ResponseString = null;
try {
URL url = new URL(URL);
URLConnection urlCon = url.openConnection();
urlCon.setDoOutput(true);
urlCon.setDoInput(true);
urlCon.setUseCaches(false);
urlCon.setRequestProperty("content-Type", "application/json");
urlCon.setRequestProperty("charset", "utf-8");
urlCon.setRequestProperty("Content-length",
String.valueOf(xmlData.length));
//System.out.println(String.valueOf(xmlData.length));
DataOutputStream printout = new DataOutputStream(
urlCon.getOutputStream());
printout.write(xmlData);
printout.flush();
printout.close();
instr = urlCon.getInputStream();
byte[] bis = IOUtils.toByteArray(instr);
ResponseString = new String(bis, "UTF-8");
if ((ResponseString == null) || ("".equals(ResponseString.trim()))) {
System.out.println("返回空");
}
//System.out.println("返回数据为:" + ResponseString);
System.out.println(ResponseString);
} catch (IOException e) {
e.printStackTrace();
}
return ResponseString;
}
}
我把代码打jar包放到服务器上运行,请求post接口
public static void main(String[] args) throws IOException {
String url="http://ip:port//getRecommendation/frequencies";
//String url="http://localhost:8085//getRecommendation/frequencies";
//
String list = doHttpPost("Test",url);
List a= Arrays.asList(list);
System.out.println(list);
}
运行
成功拿到数据了