打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
SpringBoot发送Post请求
Controller:
com.penghaisoft.wcs.huakang.controllercom.alibaba.fastjson.JSONObjectlombok.extern.slf4j.org.springframework.boot.autoconfigure.condition.org.springframework.web.bind.annotation.org.springframework.web.bind.annotation.RequestMethodorg.springframework.web.bind.annotation.org.springframework.web.bind.annotation.java.util.Map(prefix = name = havingValue = )SLWCSController {    @Autowiredpublic SLWCSService sLWCSService;/** *功能描述: 测试 */@PostMapping("/httpService/test_zx")public JSONObject testURL(@RequestParam Map map){    System.out.println("测试!");    net.sf.json.JSONObject jsonObject = sLWCSService.taskReceive(map);    return jsonObject;}}
Service
public interface SLWCSService {
/**
* 任务接收接口
*/
public JSONObject taskReceive(Map<String,String> map);
}
Service实现类:
@Service("sLWCSService")public class SLWCSServiceImpl implements SLWCSService {    //速锐接口地址    @Value(value ="${surui.surui-url}")    private String SURUI_URL;    @Autowired    public PostResponseUtil postResponseUtil;    @Override    public JSONObject taskReceive(Map<String,String> map){        //任务接收接口-路径        String url =SURUI_URL+"/taskReceive";        JSONObject jsonObject = selectJsonObject(map, url);        return jsonObject;    }
公共方法:
import net.sf.json.JSONObject;import org.springframework.http.HttpEntity;import org.springframework.http.HttpHeaders;import org.springframework.http.MediaType;import org.springframework.http.ResponseEntity;import org.springframework.stereotype.Component;import org.springframework.util.LinkedMultiValueMap;import org.springframework.util.MultiValueMap;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.client.RestTemplate;import lombok.extern.slf4j.Slf4j;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Map;import java.util.Set;/** * 发送请求公共方法 */@Slf4j@Componentpublic class PostResponseUtil {       /**     * 把json数据转化为参数,为get请求和post请求stringentity的时候使用     * @param argument 请求参数,json数据类型,map类型,可转化     * @return 返回拼接参数后的地址     */    public  String changeJsonToArguments(JSONObject argument) {        Set<String> keys = argument.keySet();        for (String key : keys) {            String value = argument.getString(key);            argument.put(key, urlEncoderText(value));        }        String one = argument.toString();        String two = "?" + one.substring(1, one.length() - 1).replace(",", "&").replace(":", "=").replace("\"", "");        //时间戳        one =two+"&timestamp="+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()).toString();        return one;    }    /**     * url进行转码,常用于网络请求     * @param text 需要加密的文本     * @return 返回加密后的文本     */    public String urlEncoderText(String text) {        String result = "";        try {            result = java.net.URLEncoder.encode(text, "utf-8");        } catch (Exception e) {            System.out.println("数据格式错误!");            e.printStackTrace();        }        return result;    }    /**     * 解析post请求的返回值     * @param wcsResp 返回的JSON值     * @return     */    public JSONObject jieXiWmsReturn(ResponseEntity<JSONObject> wcsResp) {        //获取return的信息        JSONObject jsonObject =null;        if (wcsResp.getStatusCodeValue() != 200) {            //调取接口失败            log.info("调取接口失败!");            jsonObject=null;        } else {            log.info("调WMS接口,响应成功!");            //调取接口成功,获取返回数据            JSONObject noticeResult = wcsResp.getBody();            log.info("响应数据:"+noticeResult.toString());            //String resultData = jsonObject.fromObject(noticeResult.getString("Response")).getString("return");            //log.info("响应数据-return:"+resultData);            //jsonObject = jsonObject.fromObject(resultData);            jsonObject =noticeResult;        }        return jsonObject;    }    /**     * post请求      * @param map 请求体     * @return     */    public ResponseEntity<JSONObject> getJsonObjectEntity(String url,Map<String, String> map) {        //采用JSONObject或者实体类传递参数        //创建一个响应头        HttpHeaders headers = new HttpHeaders();        //设置请求资源或数据类型:application/json  headers.setContentType(MediaType.APPLICATION_JSON)        headers.setContentType(MediaType.parseMediaType("application/json; charset=UTF-8"));        headers.add("Accept", MediaType.APPLICATION_JSON.toString());//指定客户端能够接收的内容类型,内容类型中的先后次序表示客户端接收的先后次序.        HttpEntity<Map> request = new HttpEntity<Map>(map, headers);        log.info("调用WMS接口-开始:");        //调用WMS接口        ResponseEntity<JSONObject> wcsResp = new RestTemplate().postForEntity(url, request, JSONObject.class);        log.info("调用WMS接口-结束,wcsResp:"+wcsResp);        return wcsResp;    }        /**     * 发送请求并解析JSON-欧尚     * @param map 请求体     * @param url 请求路径     * @return 返回数据     */    @ResponseBody    public JSONObject selectJsonObject(Map<String, String> map, String url) {        //post请求        ResponseEntity<String> wcsResp= getJsonEntity(url, map);        System.out.println("post请求 返回数据:"+wcsResp);        //解析        JSONObject jsonObject = jieXiWmsReturn(wcsResp);        return jsonObject;    }}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
SpringBoot:自定义注解实现后台接收Json参数
SpringCloud Gateway API接口安全设计(加密 、签名)
【Jmeter】BeanShell断言
微信公众号如何无限制的定向推送消息
接口自动化测试之http请求实践总结
java解析json数据 - Java / Web 开发
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服