<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<spring.version>5.3.6</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.11</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>com.fasterxml.jackson.core</groupId>-->
<!-- <artifactId>jackson-databind</artifactId>-->
<!-- <version>2.9.7</version>-->
<!-- </dependency>-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.22</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
</dependencies>
@Configuration
public class BeanConfig {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = BeanConfig.class)
public class RestTemplateTest {
public static final Logger log = LoggerFactory.getLogger(RestTemplateTest.class);
@Autowired
private RestTemplate restTemplate;
@Test
public void testRestTemplate() {
// 1、设置请求头
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
// 2、设置请求体
Map params = new HashMap();
params.put("customData", "123456");
params.put("priority", "0");
params.put("agentNo", "");
params.put("direction", "1");
HttpEntity httpEntity = new HttpEntity<>(params, headers);
// 发送post请求
JSONObject jsonObject = restTemplate.postForObject("http://172.16.1.134:8113/mi/addTask", httpEntity, JSONObject.class);
System.out.println(jsonObject);
String code = jsonObject.getString("code");
if(code!=null){
if("0".equals(code)){
Map map = new HashMap();
map.put("data", jsonObject.get("data"));
System.out.println(jsonObject.get("data"));
}
}
}
}
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/qq_45297578/article/details/121813961
内容来源于网络,如有侵权,请联系作者删除!