Authored by zhengyouwei

add

... ... @@ -9,15 +9,14 @@
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>monitor-ui-service</artifactId>
<artifactId>monitor-ui-common</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- 需要的Spring Framework jar包 -->
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
... ... @@ -67,7 +66,12 @@
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
... ...
package com.ui.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("config")
public class SystemConfig {
/**
* 系统环境
*/
@Value("${monitor.service.url}")
private String serviceUrl;
public String getServiceUrl() {
return serviceUrl;
}
public void setServiceUrl(String serviceUrl) {
this.serviceUrl = serviceUrl;
}
}
... ...
package com.ui.contants;
/**
* Created by zhengyouwei on 2016/6/14.
*/
public class HttpUriContants {
public static String TEST_GET_URI = "/test/get";
}
... ...
package com.ui.service.impl;
package com.ui.http;
import com.ui.service.HttpRestClientService;
import com.ui.config.SystemConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.util.Map;
@Service("httpRestClientService")
public class HttpRestClientServiceImpl implements HttpRestClientService {
public class HttpRestClient {
private static final Logger logger = LoggerFactory
.getLogger(HttpRestClientServiceImpl.class);
.getLogger(HttpRestClient.class);
@Autowired
private RestTemplate restTemplate;
@Override
public <T> T postForObject(String url, Object request, Class<T> responseType) {
private SystemConfig systemConfig;
public <T> T defaultPost(String uri, Object request, Class<T> responseType) {
try {
return restTemplate.postForObject(url, request,responseType);
return restTemplate.postForObject(systemConfig.getServiceUrl() + uri, request,responseType);
} catch (Exception e) {
logger.error("postForObject failed!url:"+url, e);
logger.error("postForObject failed!url:"+uri, e);
return null;
}
}
public void setRestTemplate(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public void setSystemConfig(SystemConfig systemConfig) {
this.systemConfig = systemConfig;
}
}
... ...
... ... @@ -37,6 +37,12 @@
</list>
</property>
</bean>
<bean id="systemConfig" class="com.ui.config.SystemConfig" />
<bean id="httpRestClient" class="com.ui.http.HttpRestClient">
<property name="restTemplate" ref="restTemplate"/>
<property name="systemConfig" ref="systemConfig"/>
</bean>
</beans>
\ No newline at end of file
... ...
... ... @@ -15,64 +15,7 @@
<dependencies>
<dependency>
<groupId>monitor-ui</groupId>
<artifactId>monitor-ui-service</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.7</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
<artifactId>monitor-ui-common</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
... ...
package com.ui.ctrl.test;
import com.alibaba.fastjson.JSONObject;
import com.ui.service.HttpRestClientService;
import com.ui.contants.HttpUriContants;
import com.ui.http.HttpRestClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
... ... @@ -18,14 +19,14 @@ import java.util.Map;
public class TestCtrl {
@Autowired
private HttpRestClientService httpRestClientService;
private HttpRestClient httpRestClient;
@RequestMapping("test")
@ResponseBody
public OrderInfoCheck test(){
Map map = new HashMap();
map.put("id",1);
OrderInfoCheck result = httpRestClientService.postForObject("http://localhost:8081/monitor/test/get",map,OrderInfoCheck.class);
OrderInfoCheck result = httpRestClient.defaultPost(HttpUriContants.TEST_GET_URI, map, OrderInfoCheck.class);
return result;
}
}
... ...
package com.ui.service;
import java.util.Map;
public interface HttpRestClientService {
public <T> T postForObject(String url, Object request, Class<T> responseType);
}
monitor.service.url=http://127.0.0.1:8080/monitor
\ No newline at end of file
... ...
... ... @@ -57,7 +57,7 @@
</dependency>
<dependency>
<groupId>monitor-ui</groupId>
<artifactId>monitor-ui-service</artifactId>
<artifactId>monitor-ui-common</artifactId>
<version>${project-version}</version>
</dependency>
</dependencies>
... ... @@ -67,7 +67,7 @@
<modules>
<module>monitor-ui-web</module>
<module>monitor-ui-ctrl</module>
<module>monitor-ui-service</module>
<module>monitor-ui-common</module>
</modules>
... ...