1、引用依赖文档:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.xibei</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.39</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2、具体内容
package com.xibei.util; import org.springframework.http.converter.StringHttpMessageConverter; import org.springframework.web.client.RestTemplate; import java.nio.charset.Charset; public class RestTemplateUtil { /** * 创建指定字符集的RestTemplate * * @param charset * @return */ public static RestTemplate getInstance(String charset) { RestTemplate restTemplate = new RestTemplate(); restTemplate.getMessageConverters().add(new StringHttpMessageConverter(Charset.forName(charset))); return restTemplate; } }
3、详细实现过程
package com.xibei; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONPathException; import com.xibei.util.RestTemplateUtil; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import java.util.List; @RestController public class DoTransferData { @RequestMapping(value = "/xibei",method = RequestMethod.GET) public String Transer() { String result =""; try { JSONObject json = new JSONObject(); JSONObject mxjson = new JSONObject(); json.put("deliverCode", "CS123456789"); json.put("deliverOrgCode", "7001211"); json.put("deliverOrgName", "北京康安利丰农业有限公司"); json.put("deliverTime", "2019-06-25 10:10:10"); json.put("retailCode", "30005"); json.put("spareField1", ""); json.put("spareField2", ""); mxjson.put("deliverPrice", "2"); mxjson.put("deliverQty", "2"); mxjson.put("delverAmount", "2"); mxjson.put("orderCode", "PO300051906190004"); mxjson.put("productCode", "11100762"); mxjson.put("productName", "猪五花肉片4*0.4(25kg)"); mxjson.put("spareDetailField1", ""); mxjson.put("spareDetailField2", ""); mxjson.put("spareDetailField3", ""); mxjson.put("spareDetailField4", ""); List list = new JSONArray(); list.add(mxjson); json.put("detailDtoList", list); HttpHeaders headers = new HttpHeaders(); headers.add("Content-Type", "application/json"); headers.add("x-auth-token", "token内容"); RestTemplate restTemplate = RestTemplateUtil.getInstance("utf-8"); HttpEntity<Object> entity = new HttpEntity<Object>(json, headers); String url = "https://apit.xibei.com/order-service/order/external/acceptReceiptItems"; result = restTemplate.postForObject(url, entity, String.class); //System.out.println(result); //System.out.println(json); } catch (JSONPathException e) { System.out.println(e.toString()); } return result; } }
转载自原文链接, 如需删除请联系管理员。
原文链接:restTemplate.postForObject 发送POST请求,转载请注明来源!