java接收回调通知实现方式

编程入门 行业动态 更新时间:2024-10-24 22:28:03

java接收<a href=https://www.elefans.com/category/jswz/34/1771356.html style=回调通知实现方式"/>

java接收回调通知实现方式

演示POST方式发送通知信息,请求参数位于request中的body中
记录一次回调参数的对接,写起来感觉很简单,但是表达就感觉特别难
文章内容仅供参考,内容有误请及时指出纠正

回调通知示例
第三方回调通知参数,我们这边需要提供一个url,用来接收这个参数。
这次的回调参数位于request中的body中
提供的地址需要是外网地址

回调地址:http://url/test/saveNotify

controller层,进行接收回调参数
程序执行完后必须打印输出“success”(不包含引号)

package com...controller.test;import com.service.jumi.JuMiAiService;
import com.web.BaseController;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
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.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.InputStreamReader;@RestController
@RequestMapping(value = "/test", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class JuMiController extends BaseController{@Autowiredprivate TestService testService;@RequestMapping(value = "saveNotify", method = RequestMethod.POST)@ApiOperation("回调参数测试")public String saveNotify(HttpServletRequest request){try {BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));String line = null;StringBuilder sb = new StringBuilder();while((line = br.readLine())!=null){sb.append(line);}String reqBody = sb.toString();testService.saveNotify(reqBody);}catch (Exception e){e.printStackTrace();}return "success";}}

service层,处理回调参数
reqBody便是第三方的回调通知参数,获取到这些参数之后便可进行其他操作处理

package com...service.test;import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;@Service
public class TestService {private Logger logger = LoggerFactory.getLogger(TestService.class);@AutowiredStringRedisTemplate redisTemplate;/*** * @param reqBody*/public void saveNotify(String reqBody){logger.info("reqBody="+reqBody);com.alibaba.fastjson.JSONObject resObj = JSONObject.parseObject(reqBody);}}

更多推荐

java接收回调通知实现方式

本文发布于:2024-02-08 20:43:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1674839.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:回调   方式   通知   java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!