クライアントがFeignを介して要求を開始するサービス側はrequestを介してjsonオブジェクトを取得する
12598 ワード
@RestController
@RequestMapping(value = "test")
public class TestServer {
@RequestMapping(value = "/receive", method = {RequestMethod.POST})
@ApiOperation(value = " ")
@ResponseBody
public void receive(HttpServletRequest request, HttpServletResponse response) {
String s = JsonReq(request);
}
//request json
public static String JsonReq(HttpServletRequest request) {
BufferedReader br;
StringBuilder sb = null;
String reqBody = null;
try {
br = new BufferedReader(new InputStreamReader(
request.getInputStream()));
String line = null;
sb = new StringBuilder();
while ((line = br.readLine()) != null) {
sb.append(line);
}
reqBody = URLDecoder.decode(sb.toString(), "UTF-8");
reqBody = reqBody.substring(reqBody.indexOf("{"));
request.setAttribute("inputParam", reqBody);
System.out.println("JsonReq reqBody>>>>>" + reqBody);
return reqBody;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "jsonerror";
}
}
}
アナログクライアント
@RestController
@RequestMapping("/feign")
public class TestFeign {
@Autowired
// FeignClient
TestApiService testApiService;
@RequestMapping("/testClient")
public void test(HttpServletRequest request){
HashMap map = new HashMap<>();
map.put("key","value");
//
testApiService.test(map);
}
}
FeignClient
@FeignClient(url = "http://localhost:8080/test/receive", name = "testApiService", primary = false, configuration = FeignConfig.class) @Headers("Accept: application/json") public interface TestApiService { /** * * @return */ @RequestLine("POST /test/receive") @Headers({"Content-Type: application/json","Accept: application/json"}) String testPost(Map req); }
FeignConfig構成クラス
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hexin.creditforce.configuration.feign.support.MapFormHttpMessageConverter;
import feign.Contract;
import feign.Logger;
import feign.Retryer;
import feign.codec.Decoder;
import feign.codec.Encoder;
import feign.form.spring.SpringFormEncoder;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.support.ResponseEntityDecoder;
import org.springframework.cloud.openfeign.support.SpringDecoder;
import org.springframework.cloud.openfeign.support.SpringEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
/**
* feign config
*/
@Configuration
public class FeignConfig {
@Autowired
private ObjectFactory messageConverters;
@Bean
Logger.Level feignLoggerLevel() {
return Logger.Level.FULL;
}
@Bean
public Contract feignContract() {
return new Contract.Default();
}
@Bean
Retryer retryer() {
return Retryer.NEVER_RETRY;
}
/**
* form , form
*/
@Bean
public Encoder feignFormEncoder() {
MappingJackson2HttpMessageConverter jacksonConverter = new MappingJackson2HttpMessageConverter(customObjectMapper());
List> converters = new ArrayList<>();
for (HttpMessageConverter> converter : messageConverters.getObject().getConverters()) {
if (!(converter instanceof MappingJackson2HttpMessageConverter)) {
converters.add(converter);
}
}
converters.add(jacksonConverter);
converters.add( new MapFormHttpMessageConverter() );
return new SpringFormEncoder(new SpringEncoder(() -> new HttpMessageConverters(converters)));
}
@Bean
public Decoder feignDecoder() {
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
List supportedMediaTypes = new ArrayList<>( );
supportedMediaTypes.add( MediaType.ALL );
converter.setSupportedMediaTypes( supportedMediaTypes );
ObjectFactory objectFactory = () -> new HttpMessageConverters(converter);
return new ResponseEntityDecoder(new SpringDecoder(objectFactory));
}
private ObjectMapper customObjectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
return objectMapper;
}
}
pom
org.springframework.cloud
spring-cloud-starter-openfeign
org.springframework.boot
spring-boot-starter-actuator
io.github.openfeign.form
feign-form
io.github.openfeign.form
feign-form-spring
転載先:https://www.cnblogs.com/java-xz/p/10642986.html