RestTemplate 工具类

x33g5p2x  于2021-12-09 转载在 其他  
字(2.9k)|赞(0)|评价(0)|浏览(601)

pom

<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>

创建RestTemplate组件

@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"));
           }
       }
   }
}

相关文章