@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"; } } }
模擬客戶端java
@RestController @RequestMapping("/feign") public class TestFeign { @Autowired //注入FeignClient服務類 TestApiService testApiService; @RequestMapping("/testClient") public void test(HttpServletRequest request){ HashMap<String, String> 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<String, String> req); }
FeignConfig配置類git
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<HttpMessageConverters> 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<HttpMessageConverter<?>> 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<MediaType> supportedMediaTypes = new ArrayList<>( ); supportedMediaTypes.add( MediaType.ALL ); converter.setSupportedMediaTypes( supportedMediaTypes ); ObjectFactory<HttpMessageConverters> 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配置
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency> <groupId>io.github.openfeign.form</groupId> <artifactId>feign-form</artifactId></dependency><dependency> <groupId>io.github.openfeign.form</groupId> <artifactId>feign-form-spring</artifactId></dependency>