一:github地址

二:依赖
1 2 3 4 5
| <dependency> <groupId>com.mashape.unirest</groupId> <artifactId>unirest-java</artifactId> <version>1.4.4</version> </dependency>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.3.6</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpasyncclient</artifactId> <version>4.0.2</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpmime</artifactId> <version>4.3.6</version> </dependency> <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20140107</version> </dependency>
|
1 2 3 4 5 6
| <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency>
|
三:初始化配置
1 2 3
| 主要是设置一下超时时间和序列化,如果不设置序列化, 那个Unirest.post().body()中的body不能传递json对象, 针对一些接口会参数缺失。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| package com.tohours.dwx.config;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration;
import com.fasterxml.jackson.core.JsonProcessingException; import com.mashape.unirest.http.ObjectMapper; import com.mashape.unirest.http.Unirest;
/** * * @author RenJie * */ @Configuration public class UnirestConfig {
@Autowired private com.fasterxml.jackson.databind.ObjectMapper mapper;
@PostConstruct public void postConstruct() { // 1.0 设置超时时间 // Unirest默认为10s Integer connectionTimeout = 5 * 1000; // Unirest默认为60s Integer socketTimeout = 20 * 1000; Unirest.setTimeouts(connectionTimeout, socketTimeout);
// 2.0 设置jackjsonMapper,实现unirest序列化 Unirest.setObjectMapper(new ObjectMapper() {
public String writeValue(Object value) { try { return mapper.writeValueAsString(value); } catch (JsonProcessingException e) { throw new RuntimeException(e); } }
public <T> T readValue(String value, Class<T> valueType) { try { return mapper.readValue(value, valueType); } catch (Exception e) { throw new RuntimeException(e); } } }); } }
|
*************感谢您的阅读*************