SSM —— SpringMvc的每一个控制器都可以做为http服务器

╰半橙微兮° 2023-11-10 06:16 161阅读 0赞

SpringMvc不仅可以接收Jsp页面的请求,而且可以作为一个简易的Http服务器,用控制器去接收别的http请求,进行业务操作最后返回数据,因为jsp页面请求其实也是http请求:

SSM框架搭建参考另一篇博客https://blog.csdn.net/DGH2430284817/article/details/88587804

控制器:

  1. package com.test.controller;
  2. import java.net.UnknownHostException;
  3. import org.apache.log4j.Logger;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.stereotype.Controller;
  6. import org.springframework.ui.Model;
  7. import org.springframework.web.bind.annotation.RequestBody;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import org.springframework.web.bind.annotation.RequestMethod;
  10. import org.springframework.web.bind.annotation.ResponseBody;
  11. import com.ceb.dgh.bo.Door;
  12. import com.qut.util.LoggerUtil;
  13. import com.test.service.DoorService;
  14. @RequestMapping("/door")
  15. @Controller
  16. public class DoorController {
  17. private static Logger log = LoggerUtil.getLogger(DoorController.class);
  18. //我就不注入了,没用到数据库和业务逻辑
  19. //@Autowired
  20. //private DoorService doorService;
  21. @RequestMapping(value = "server",method = {RequestMethod.POST})
  22. @ResponseBody
  23. public String serverTest(@RequestBody String clientMsg){
  24. log.info("进入控制器server,参数:" + clientMsg);
  25. //业务操作省略
  26. String returnMsg = "服务器返回客户端数据";//相当于业务操作结果
  27. return returnMsg;
  28. }
  29. }

http客户端TestClient.java:

  1. package test.client;
  2. import java.io.BufferedReader;
  3. import java.io.InputStreamReader;
  4. import java.io.OutputStream;
  5. import java.net.HttpURLConnection;
  6. import java.nio.charset.Charset;
  7. import java.net.URL;
  8. import org.springframework.util.Assert;
  9. public class TestClient {
  10. public static void main(String[] args) throws Exception {
  11. TestClient aaClient = new TestClient();
  12. String Msg = "client sent to server";
  13. String resString =aaClient.doPostRequest("http://localhost:8080/Maven_SSM/door/server.action", Msg, Charset.forName("UTF-8"), 10, 10);
  14. System.out.println("返回数据:"+resString);
  15. }
  16. /**
  17. * @param reqUrl 地址
  18. * @param params 数据(json)
  19. * @param charset 编码
  20. * @param readTimeSec
  21. * @param connTimeSec
  22. * @return Http请求返回数据字符串
  23. * @throws Exception
  24. */
  25. public static String doPostRequest(String reqUrl, String params,
  26. Charset charset, int readTimeSec, int connTimeSec)
  27. throws Exception {
  28. try {
  29. Assert.hasText(reqUrl, "请求地址为空");
  30. Assert.hasText(params, "请求数据为空");
  31. } catch (Exception e) {
  32. throw new Exception("发送请求参数为空:"
  33. + e.getMessage(), e);
  34. }
  35. String request = params ;//发送服务端数据
  36. HttpURLConnection httpConnection = null;
  37. try {
  38. httpConnection = (HttpURLConnection) new URL(reqUrl)
  39. .openConnection();
  40. httpConnection.setRequestMethod("POST");
  41. httpConnection.setDoOutput(true);
  42. httpConnection.setDoInput(true);
  43. httpConnection.setUseCaches(false);
  44. httpConnection.setRequestProperty("Connection", "Keep-Alive");
  45. httpConnection.setRequestProperty("Charset", charset.displayName());
  46. httpConnection.setRequestProperty("Content-Type",
  47. "application/json; charset=" + charset.displayName());
  48. httpConnection.setRequestProperty("accept", "application/json");
  49. httpConnection.setReadTimeout(readTimeSec * 1000);// 设置http连接的读超时,单位是毫秒
  50. httpConnection.setConnectTimeout(connTimeSec * 1000);
  51. // 1、连接
  52. try {
  53. httpConnection.connect();
  54. } catch (Exception e) {
  55. throw new Exception(
  56. "服务器连接是失败");
  57. }
  58. OutputStream outwritestream = null;
  59. // 2、发送数据
  60. try {
  61. System.out.println("请求数据:"+request);
  62. byte[] writebytes = request.getBytes(charset);
  63. outwritestream = httpConnection.getOutputStream();
  64. outwritestream.write(writebytes);
  65. outwritestream.flush();
  66. } catch (Exception e) {
  67. throw new Exception(
  68. "请求数据发送失败", e);
  69. } finally {
  70. try {
  71. outwritestream.close();
  72. } catch (Exception e) {
  73. }
  74. }
  75. return getHttpReturn(httpConnection , charset);
  76. } catch (Exception e) {
  77. throw new Exception("服务器连接失败:"
  78. + e.getMessage(), e);
  79. } finally {
  80. if (httpConnection != null) {
  81. httpConnection.disconnect();
  82. }
  83. }
  84. }
  85. /**
  86. * @param httpConnection
  87. * @return Http连接返回数据转化成字符串
  88. * @throws Exception
  89. */
  90. private static String getHttpReturn(HttpURLConnection httpConnection , Charset charset)
  91. throws Exception {
  92. int responseCode = -1;
  93. try {
  94. responseCode = httpConnection.getResponseCode();
  95. //System.out.println("responseCode:"+responseCode);
  96. } catch (Exception e) {
  97. throw new Exception(
  98. "接收数据超时", e);
  99. }
  100. try {
  101. Assert.isTrue(responseCode == 200, "服务器响应码【"
  102. + responseCode + "】");
  103. } catch (Exception e) {
  104. throw new Exception(
  105. "接收数据失败:" + e.getMessage(), e);
  106. }
  107. BufferedReader reader = null;
  108. try {
  109. reader = new BufferedReader(new InputStreamReader(
  110. httpConnection.getInputStream(), charset));
  111. String str = "";
  112. String temp;
  113. while((temp = reader.readLine())!=null){
  114. str = str + temp +"\r\n" ;
  115. }
  116. return str;
  117. } catch (Exception e) {
  118. throw new Exception(
  119. "接收数据超时:" + e.getMessage(), e);
  120. } finally {
  121. try {
  122. if (null != reader) {
  123. reader.close();
  124. }
  125. } catch (Exception e) {
  126. }
  127. }
  128. }
  129. }

开启SSM项目后右键启动客户端类,发起http请求:

测试结果:

服务端:

  1. [2019-03-18 18:03:17,955] [INFO] [com.test.controller.DoorController.httpMo:40] - 进入控制器server,参数:client sent to server

客户端:

  1. 请求数据:client sent to server
  2. 返回数据:服务器返回客户端数据

发表评论

表情:
评论列表 (有 0 条评论,161人围观)

还没有评论,来说两句吧...

相关阅读