修改restTemplate获取数据的方式。同时修改默认请求json格式数据。
This commit is contained in:
+16
-15
@@ -1,16 +1,20 @@
|
|||||||
package com.gzzn.omms.msgexchangeapi.basicdata.service;
|
package com.gzzn.omms.msgexchangeapi.basicdata.service;
|
||||||
|
|
||||||
import java.util.LinkedHashMap;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.apache.lucene.analysis.ar.ArabicAnalyzer;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.core.ParameterizedTypeReference;
|
||||||
|
import org.springframework.http.HttpEntity;
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
|
import org.springframework.http.HttpMethod;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
||||||
import com.gzzn.omms.msgexchangeapi.basicdata.entity.OrmsStand;
|
import com.gzzn.omms.msgexchangeapi.basicdata.entity.OrmsStand;
|
||||||
import com.gzzn.omms.msgexchangeapi.dto.ResponseDto;
|
import com.gzzn.omms.msgexchangeapi.dto.ResponseDto;
|
||||||
|
|
||||||
@@ -28,19 +32,16 @@ public class OrmsStandServiceImpl implements IOrmsStandService{
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<OrmsStand> findAll() {
|
public List<OrmsStand> findAll() {
|
||||||
ResponseEntity<ResponseDto> responseEntity = restTemplate
|
ResponseEntity<ResponseDto<List<OrmsStand>>> responseEntity = restTemplate
|
||||||
.getForEntity(
|
.exchange(ormsStandsUrl,
|
||||||
ormsStandsUrl,
|
HttpMethod.GET,
|
||||||
ResponseDto.class);
|
null,
|
||||||
ResponseDto responseDto = responseEntity.getBody();
|
new ParameterizedTypeReference<ResponseDto<List<OrmsStand>>>() {
|
||||||
|
});
|
||||||
|
|
||||||
|
ResponseDto<List<OrmsStand>> responseDto = responseEntity.getBody();
|
||||||
if (responseDto.getIs_success()) {
|
if (responseDto.getIs_success()) {
|
||||||
List<LinkedHashMap> lsLinked = (List<LinkedHashMap>)responseDto.getBody();
|
return responseDto.getBody();
|
||||||
|
|
||||||
//
|
|
||||||
ObjectMapper mapper = new ObjectMapper();
|
|
||||||
List<OrmsStand> rt = mapper.convertValue(lsLinked, new TypeReference<List<OrmsStand>>() { });
|
|
||||||
|
|
||||||
return rt;
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
throw new RuntimeException("未知错误"+responseDto.getErr_msg());
|
throw new RuntimeException("未知错误"+responseDto.getErr_msg());
|
||||||
|
|||||||
@@ -1,10 +1,13 @@
|
|||||||
package com.gzzn.omms.msgexchangeapi.config;
|
package com.gzzn.omms.msgexchangeapi.config;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
|
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.http.client.ClientHttpRequestFactory;
|
import org.springframework.http.client.ClientHttpRequestFactory;
|
||||||
import org.springframework.http.client.SimpleClientHttpRequestFactory;
|
import org.springframework.http.client.SimpleClientHttpRequestFactory;
|
||||||
|
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
||||||
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@@ -12,7 +15,10 @@ public class RestTemplateConfig {
|
|||||||
@Bean
|
@Bean
|
||||||
@LoadBalanced
|
@LoadBalanced
|
||||||
public RestTemplate restTemplate(ClientHttpRequestFactory factory){
|
public RestTemplate restTemplate(ClientHttpRequestFactory factory){
|
||||||
return new RestTemplate(factory);
|
RestTemplate rest = new RestTemplate(factory);
|
||||||
|
rest.setMessageConverters(Arrays.asList(new MappingJackson2HttpMessageConverter()));
|
||||||
|
|
||||||
|
return rest;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
|||||||
@@ -88,6 +88,7 @@ elasticsearch:
|
|||||||
#注意cluster.name需要与config/elasticsearch.yml中的cluster.name一致
|
#注意cluster.name需要与config/elasticsearch.yml中的cluster.name一致
|
||||||
cluster:
|
cluster:
|
||||||
name: docker-cluster
|
name: docker-cluster
|
||||||
|
nodes: 130.120.3.234:9300,130.120.3.236:9300,130.120.3.238:9300
|
||||||
eureka:
|
eureka:
|
||||||
client:
|
client:
|
||||||
service-url:
|
service-url:
|
||||||
|
|||||||
@@ -1,16 +1,22 @@
|
|||||||
package com.gzzn.omms.msgexchangeapi.config;
|
package com.gzzn.omms.msgexchangeapi.config;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
import org.springframework.boot.test.context.TestConfiguration;
|
import org.springframework.boot.test.context.TestConfiguration;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.http.client.ClientHttpRequestFactory;
|
import org.springframework.http.client.ClientHttpRequestFactory;
|
||||||
import org.springframework.http.client.SimpleClientHttpRequestFactory;
|
import org.springframework.http.client.SimpleClientHttpRequestFactory;
|
||||||
|
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
||||||
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
@TestConfiguration
|
@TestConfiguration
|
||||||
public class TestRestTemplateConfig {
|
public class TestRestTemplateConfig {
|
||||||
@Bean
|
@Bean
|
||||||
public RestTemplate restTemplate(ClientHttpRequestFactory factory){
|
public RestTemplate restTemplate(ClientHttpRequestFactory factory){
|
||||||
return new RestTemplate(factory);
|
RestTemplate rest = new RestTemplate(factory);
|
||||||
|
rest.setMessageConverters(Arrays.asList(new MappingJackson2HttpMessageConverter()));
|
||||||
|
|
||||||
|
return rest;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
|||||||
Reference in New Issue
Block a user