客户端通过Feign发起请求 服务端通过request取 json对象

@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

你可能感兴趣的:(客户端通过Feign发起请求 服务端通过request取 json对象)