Feign 发送XML报文相关的问题及编码问题

1.问题:报文要求使用GBK编码,返回的statusText字段中也有中文,如果使用默认的FeignClient发送请求是没有办法的

@FeignClient(name = "testFeign",url="127.0.0.1:6789" ,configuration = FeignDecoder.class)
public interface TestFeign {
    @PostMapping(consumes = MediaType.APPLICATION_XML_VALUE,produces = MediaType.APPLICATION_XML_VALUE)
    String getBalance(@RequestBody String params);

}
consumes:该接口只接受指定的数据类型
produces:指定返回的数据类型,指定为xml的类型

2:返回字段中设置GBK编码

继承Feign的Decoder接口,示例如下:

@Override
public Object decode(Response response, Type type) throws IOException, DecodeException, FeignException {
    try(InputStream inputStream = response.body().asInputStream()) {
        byte[] bodyBytes = IOUtils.toByteArray(inputStream);
        String bodyStrArr = new String(bodyBytes, "GBK");
        return bodyStrArr;
    }
}

3:请求编码的话 ,我改都不行,最后没有办法了,使用的是RestTemplate的封装了一个 重写了HttpMessageConverter才行的

4:疑惑如下:

实现Encoder接口的话,好像没有用,

实现RequestInterceptor接口的话,重新设置header,包括设置RequestCharset的encoder方法都没有用,有知道的大哥可以联系我

按理说我再xml的头部中指定了<?xml version="1.0" encoding="GBK"?>编码,还是用的是application/xml这种,应该报文里的中文是按照GBK进行编码的,但是他好像还是按照UTF-8进行编码的,这个地方也没明白,有了解的也可以联系我

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用Feign传递LocalDate格式的问题上,我们需要进行一些额外的处理。 首先,Feign默认使用Java的日期转换机制,它将使用标准的`java.time.format.DateTimeFormatter.ISO_LOCAL_DATE`格式将LocalDate转换为字符串。因此,我们需要确保服务提供者和消费者使用相同的格式。 其次,Feign不支持直接传递LocalDate对象。我们可以通过自定义一个转换器来实现将LocalDate转换为指定格式的字符串,并将其添加到Feign的配置中。 下面是一个使用自定义转换器的示例: 1. 创建一个实现`Encoder`接口的自定义转换器类,例如`LocalDateEncoder`: ```java import feign.RequestTemplate; import feign.codec.Encoder; public class LocalDateEncoder implements Encoder { private final Encoder delegate; public LocalDateEncoder(Encoder delegate) { this.delegate = delegate; } @Override public void encode(Object object, Type bodyType, RequestTemplate template) throws EncodeException { if (object instanceof LocalDate) { // 将LocalDate转换为指定格式的字符串 LocalDate localDate = (LocalDate) object; String formattedDate = localDate.format(DateTimeFormatter.ISO_LOCAL_DATE); // 将字符串设置为请求的body template.body(formattedDate); } else { delegate.encode(object, bodyType, template); } } } ``` 2. 创建一个使用自定义转换器的Feign配置类: ```java import feign.codec.Encoder; @Configuration public class FeignConfig { @Autowired private ObjectFactory<HttpMessageConverters> messageConverters; @Bean public Encoder feignEncoder() { return new LocalDateEncoder(new SpringEncoder(messageConverters)); } } ``` 3. 在服务提供者和消费者的配置件中分别注入Feign配置类: ```java @Import(FeignConfig.class) @SpringBootApplication public class YourApplication { // ... } ``` 通过以上步骤,我们就能够在Feign中传递LocalDate格式的数据了。请注意,在使用自定义转换器之前,一定要确保服务提供者和消费者都已经正确配置了`java.time.LocalDate`的支持。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值