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