springboot向指定接口推送消息

您所在的位置:网站首页 如何向指定ip发信息 springboot向指定接口推送消息

springboot向指定接口推送消息

2024-07-11 08:30:41| 来源: 网络整理| 查看: 265

一 说明

本案例使用http的post请求实现向指定接口推送消息。

发送端这里需要注意:

二. 操作案例 2.1客户端(push端) 2.1.1 工程结构

2.1.2 pom文件 junit junit 4.11 test org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.apache.httpcomponents httpcore 4.4.10 com.alibaba fastjson 1.2.47 org.apache.httpcomponents httpclient 4.5.6 org.slf4j slf4j-api 1.7.7 2.1.3 controller package com.ljf.spring.boot.demo.controller; import com.alibaba.fastjson.JSON; import com.fasterxml.jackson.annotation.JsonFormat; import com.ljf.spring.boot.demo.http.HttpService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; /** * @ClassName: UserController * @Description: TODO * @Author: liujianfu * @Date: 2021/03/11 22:36:37  * @Version: V1.0 **/ @RestController public class UserController { @Autowired private HttpService httpService; @RequestMapping("/push") public void Login(@RequestParam(value = "date", required = false) String date, @RequestParam(value = "type", required = false) String type, @RequestParam(value = "xt", required = false) String[] xt){ System.out.println("进入controller了,第四方!!!!!"); String v="[{\"DeviceName\":\"悬臂堆料机\",\"deviceCode\":\"TCNY_JEK_DQLJ_2_001\",\"measurePointList\":[{\"measurePointCode\":\"EQCQ_TCNY_NK_SLD121_M1_RUN\",\"measurePointName\":\"堆料臂回转角度\",\"timeStamp\":1615513986828,\"angleVal\":1.0},{\"measurePointCode\":\"EQCQ_TCNY_NK_SLD121_M2_RUN\",\"measurePointName\":\"堆料皮带运行指示\",\"timeStamp\":1615513986828,\"angleVal\":1.0}]},{\"DeviceName\":\"俯仰机构\",\"deviceCode\":\"TCNY_JEK_DQLJ_2_014\",\"measurePointList\":[{\"measurePointCode\":\"EQCQ_TCNY_NK_SLD121_M3_RUN\",\"measurePointName\":\"取料倾角\",\"timeStamp\":1615513986828,\"angleVal\":1.0},{\"measurePointCode\":\"EQCQ_TCNY_NK_SLD122_M1_RUN\",\"measurePointName\":\" 取料回转角\",\"timeStamp\":1615513986828,\"angleVal\":1.0}]},{\"DeviceName\":\"刮板取料机\",\"deviceCode\":\"TCNY_JEK_DQLJ_2_006\",\"measurePointList\":[{\"measurePointCode\":\"EQCQ_TCNY_NK_SLD122_M2_RUN\",\"measurePointName\":\"刮板运行指示\",\"timeStamp\":1615513987828,\"angleVal\":1.0}]}]"; // String j= JSON.toJSONString(v); // System.out.println("j:"+j); v="{\"data\":"+v+"}"; System.out.println("v:"+v); httpService.sendMsg("http://localhost:8080/pulldata",v); } } 2.1.4 http的post 1.HttpClientBuilder的编写 package com.ljf.spring.boot.demo.http; import org.apache.http.Header; import org.apache.http.client.config.RequestConfig; import org.apache.http.message.BasicHeader; /** * 默认的请求参数 * @Author LiYangYang */ public class HttpClientBuilder { private static final Header[] headers = new Header[]{ new BasicHeader("charset", "utf-8"), new BasicHeader("Content-Type", "application/json") }; /** * 获得一个默认的请求配置 * @return RequestConfig */ public static RequestConfig getDefaultRequestConfig(){ return RequestConfig.custom() .setConnectionRequestTimeout(2000) .setConnectTimeout(2000) .build(); } /** * 简单头信息获取 * @return 请求头数组 */ public static Header[] getHeaders(){ return headers; } } 2.HttpService package com.ljf.spring.boot.demo.http; import com.alibaba.fastjson.JSON; import org.apache.http.HttpEntity; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; import java.io.IOException; /** * 数据推送服务类 * 推送json格式数据 */ @Service("localHttpService") public class HttpService { private static final Logger logger = LoggerFactory.getLogger(HttpService.class); /** * 发送数据到指定的地址 * @param url 发送地址 * @param body 发送内容 */ public void sendMsg(String url, Object body){ RequestConfig defaultRequestConfig = RequestConfig.custom() .setSocketTimeout(2000) .setConnectTimeout(2000) .setConnectionRequestTimeout(5000) .build(); CloseableHttpClient httpClient = HttpClients.custom() .setDefaultRequestConfig(defaultRequestConfig) .build(); // CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost post = new HttpPost(url); post.setHeaders(HttpClientBuilder.getHeaders()); post.setConfig(HttpClientBuilder.getDefaultRequestConfig()); HttpEntity entry = new StringEntity(JSON.toJSONString(body) ,"UTF-8"); post.setEntity(entry); CloseableHttpResponse response; try { response = httpClient.execute(post); response.close(); } catch (IOException e) { e.printStackTrace(); logger.error("发送数据失败, {}", e.getMessage()); } } /** * 发送数据到指定的地址 * @param url 发送地址 * @param body 发送内容 */ public void sendMsg(String url, String body){ RequestConfig defaultRequestConfig = RequestConfig.custom() .setSocketTimeout(2000) .setConnectTimeout(2000) .setConnectionRequestTimeout(5000) .build(); CloseableHttpClient httpClient = HttpClients.custom() .setDefaultRequestConfig(defaultRequestConfig) .build(); // CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost post = new HttpPost(url); post.setHeaders(HttpClientBuilder.getHeaders()); post.setConfig(HttpClientBuilder.getDefaultRequestConfig()); HttpEntity entry = new StringEntity(body, "UTF-8"); post.setEntity(entry); CloseableHttpResponse response; try { response = httpClient.execute(post); response.close(); } catch (IOException e) { e.printStackTrace(); logger.error("发送数据失败, {}", e.getMessage()); } } } 2.1.5 启动类 package com.ljf.spring.boot.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * Hello world! * */ @SpringBootApplication public class ClientApp { public static void main( String[] args ){ SpringApplication.run(ClientApp.class); System.out.println("=======================客户启动完成!!!"); } } 2.2  服务端(pull端) 2.2.1 工程结构

2.2.2 pom文件的配置 junit junit 4.11 test org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test io.springfox springfox-swagger2 2.9.2 io.springfox springfox-swagger-ui 2.9.2 com.alibaba fastjson 1.2.47 com.google.code.gson gson 2.8.5 2.2.3 controller的接收 package com.ljf.spring.boot.demo.controller; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.gson.JsonObject; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.MissingServletRequestParameterException; import org.springframework.web.bind.annotation.*; import java.io.IOException; import java.util.Map; /** * @ClassName: UserController * @Description: TODO * @Author: liujianfu * @Date: 2021/03/11 17:33:38  * @Version: V1.0 **/ @RestController @Api(value = "调用用户信息controller") //用在类上,说明该类的作用。 public class UserController { @RequestMapping("/pulldata") public void pullData(@RequestBody JSONObject obj) throws IOException { // JSONArray msg= JSONObject.parseArray(obj.toString()); System.out.println("结束到的数据:"+obj); Map infoMap = new ObjectMapper().readValue(obj.toString(), Map.class); //json转换成map System.out.println("infoMap:"+infoMap); // System.out.println("msg:"+msg); } } 2.2.4 启动类 package com.ljf.spring.boot.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * Hello world! * */ @SpringBootApplication public class ServerApp { public static void main( String[] args ){ SpringApplication.run(ServerApp.class); System.out.println("=======================服务启动完成!!!"); } } 2.3 模拟发送数据

客户端访问:客户端的端口为8081 ,启动访问的地址为:http://localhsot:8081/push   推送给服务端的地址为:http://localhost:8080/pulldata

服务端接收:端口号为8080,接收的方法为pulldata,可以看到服务成功接收到数据



【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭