SSM —— SpringMvc的每一个控制器都可以做为http服务器
SpringMvc不仅可以接收Jsp页面的请求,而且可以作为一个简易的Http服务器,用控制器去接收别的http请求,进行业务操作最后返回数据,因为jsp页面请求其实也是http请求:
SSM框架搭建参考另一篇博客https://blog.csdn.net/DGH2430284817/article/details/88587804
控制器:
package com.test.controller;
import java.net.UnknownHostException;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ceb.dgh.bo.Door;
import com.qut.util.LoggerUtil;
import com.test.service.DoorService;
@RequestMapping("/door")
@Controller
public class DoorController {
private static Logger log = LoggerUtil.getLogger(DoorController.class);
//我就不注入了,没用到数据库和业务逻辑
//@Autowired
//private DoorService doorService;
@RequestMapping(value = "server",method = {RequestMethod.POST})
@ResponseBody
public String serverTest(@RequestBody String clientMsg){
log.info("进入控制器server,参数:" + clientMsg);
//业务操作省略
String returnMsg = "服务器返回客户端数据";//相当于业务操作结果
return returnMsg;
}
}
http客户端TestClient.java:
package test.client;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.nio.charset.Charset;
import java.net.URL;
import org.springframework.util.Assert;
public class TestClient {
public static void main(String[] args) throws Exception {
TestClient aaClient = new TestClient();
String Msg = "client sent to server";
String resString =aaClient.doPostRequest("http://localhost:8080/Maven_SSM/door/server.action", Msg, Charset.forName("UTF-8"), 10, 10);
System.out.println("返回数据:"+resString);
}
/**
* @param reqUrl 地址
* @param params 数据(json)
* @param charset 编码
* @param readTimeSec
* @param connTimeSec
* @return Http请求返回数据字符串
* @throws Exception
*/
public static String doPostRequest(String reqUrl, String params,
Charset charset, int readTimeSec, int connTimeSec)
throws Exception {
try {
Assert.hasText(reqUrl, "请求地址为空");
Assert.hasText(params, "请求数据为空");
} catch (Exception e) {
throw new Exception("发送请求参数为空:"
+ e.getMessage(), e);
}
String request = params ;//发送服务端数据
HttpURLConnection httpConnection = null;
try {
httpConnection = (HttpURLConnection) new URL(reqUrl)
.openConnection();
httpConnection.setRequestMethod("POST");
httpConnection.setDoOutput(true);
httpConnection.setDoInput(true);
httpConnection.setUseCaches(false);
httpConnection.setRequestProperty("Connection", "Keep-Alive");
httpConnection.setRequestProperty("Charset", charset.displayName());
httpConnection.setRequestProperty("Content-Type",
"application/json; charset=" + charset.displayName());
httpConnection.setRequestProperty("accept", "application/json");
httpConnection.setReadTimeout(readTimeSec * 1000);// 设置http连接的读超时,单位是毫秒
httpConnection.setConnectTimeout(connTimeSec * 1000);
// 1、连接
try {
httpConnection.connect();
} catch (Exception e) {
throw new Exception(
"服务器连接是失败");
}
OutputStream outwritestream = null;
// 2、发送数据
try {
System.out.println("请求数据:"+request);
byte[] writebytes = request.getBytes(charset);
outwritestream = httpConnection.getOutputStream();
outwritestream.write(writebytes);
outwritestream.flush();
} catch (Exception e) {
throw new Exception(
"请求数据发送失败", e);
} finally {
try {
outwritestream.close();
} catch (Exception e) {
}
}
return getHttpReturn(httpConnection , charset);
} catch (Exception e) {
throw new Exception("服务器连接失败:"
+ e.getMessage(), e);
} finally {
if (httpConnection != null) {
httpConnection.disconnect();
}
}
}
/**
* @param httpConnection
* @return Http连接返回数据转化成字符串
* @throws Exception
*/
private static String getHttpReturn(HttpURLConnection httpConnection , Charset charset)
throws Exception {
int responseCode = -1;
try {
responseCode = httpConnection.getResponseCode();
//System.out.println("responseCode:"+responseCode);
} catch (Exception e) {
throw new Exception(
"接收数据超时", e);
}
try {
Assert.isTrue(responseCode == 200, "服务器响应码【"
+ responseCode + "】");
} catch (Exception e) {
throw new Exception(
"接收数据失败:" + e.getMessage(), e);
}
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(
httpConnection.getInputStream(), charset));
String str = "";
String temp;
while((temp = reader.readLine())!=null){
str = str + temp +"\r\n" ;
}
return str;
} catch (Exception e) {
throw new Exception(
"接收数据超时:" + e.getMessage(), e);
} finally {
try {
if (null != reader) {
reader.close();
}
} catch (Exception e) {
}
}
}
}
开启SSM项目后右键启动客户端类,发起http请求:
测试结果:
服务端:
[2019-03-18 18:03:17,955] [INFO] [com.test.controller.DoorController.httpMo:40] - 进入控制器server,参数:client sent to server
客户端:
请求数据:client sent to server
返回数据:服务器返回客户端数据
还没有评论,来说两句吧...