如何在添加了jackson-dataformat-xml的情况下将默认MessageConverter设置为JSON? [英] How to set default MessageConverter to JSON with jackson-dataformat-xml added?

查看:861
本文介绍了如何在添加了jackson-dataformat-xml的情况下将默认MessageConverter设置为JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用JSON作为交换数据格式的正常工作的Spring Boot应用程序.现在,我必须添加一个仅以xml发送其数据的服务.我将jackson-dataformat-xml添加到了pom中,并且效果很好.

I have a working spring boot application that uses JSON as exchange data format. Now I had to add a service that sends their data only in xml. I added jackson-dataformat-xml to my pom and it worked perfectly.

@Service
public class TemplateService {

    private final RestTemplate restTemplate;
    private final String serviceUri;

    public TemplateService (RestTemplate restTemplate, @Value("${service.url_templates}") String serviceUri) {
        this.restTemplate = restTemplate;
        this.serviceUri = serviceUri;
    }

    public boolean createTemplate(Template template) {
        HttpHeaders headers = new HttpHeaders();
        headers.setAccept(Collections.singletonList(MediaType.APPLICATION_XML));
        headers.setContentType(MediaType.APPLICATION_XML);
        HttpEntity entity = new HttpEntity<>(template, headers);
        ResponseEntity<Template> response = restTemplate.exchange(serviceUri, HttpMethod.POST, entity, Template.class);
        if (response.getStatusCode().is2xxSuccessful()) {
            // do some stuff
            return true;
        }
        return false;
    }
}

现在不幸的是,添加依赖项后,我所有其他POST方法默认都发送XML.或者Content设置为application/xml.但是我想在这里使用JSON.

Now unfortunately after adding the dependency all my other POST methods send XML by default. Or the Content is set to application/xml.But I'd like to have JSON here.

@Service
public class SomeOtherService{

    private final RestTemplate restTemplate;
    private final String anotherUri;

    public SomeOtherService(RestTemplate restTemplate, @Value("${anotherUri.url}") String anotherUri) {
        this.restTemplate = restTemplate;
        this.anotherUri = anotherUri;
    }

    public ComponentEntity doSomething(String projectId, MyNewComponent newComponent) {
        ResponseEntity<MyNewComponent> result = this.restTemplate.exchange(anotherUri ,HttpMethod.POST, new HttpEntity<>(newComponent), MyNewComponent.class);
    //...
    }
}

我没有显式设置标头,因为有许多POST请求,并且我不想更改所有标头.是否可以将默认 Content设置为JSON?

I did not set the headers explicitly as there are lots of POST requests and I don't want to alter them all. Is there a way to set the default Content to JSON?

到目前为止,我已经尝试过

So far I tried

  1. 添加拦截器.但是有时候我需要XML.
  2. 覆盖内容协商

    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        configurer.defaultContentType(MediaType.APPLICATION_JSON);
    }

  1. 设置

restTemplate.setMessageConverters(Collections.singletonList(new MappingJackson2HttpMessageConverter()));

,并在要使用XML的服务中使用new RestTemplate().

and using a new RestTemplate() in the service where I want to have XML.

==>数字3确实有效,但感觉有点不对.

==> Number 3 actually works, but feels kind of wrong.

我希望在某处设置默认的Content类型,以便在没有设置任何内容的XML和我明确将Content设置为XML的XML中使用

I was expecting to set the default Content type somewhere so that JSON is used in normal cases where nothing is set and XML where I explicitly set the Content to XML.

感谢您的帮助.

推荐答案

我们最终发现,消息转换器的顺序非常重要. Jackson似乎将XML消息转换器放在JSON消息转换器之前.因此,我们将XML消息转换移动到末尾,并且它可以正常工作.

What we ultimately found out, is that the the order of the message converters is highly important. Jackson seems to place the XML message converter before the JSON message converter. So we moved the XML message convert to the end and it worked.

    @Bean
    RestTemplate restTemplate() {
    RestTemplate restTemplate = new RestTemplate();
    // move XML converter to the end of list
    List<HttpMessageConverter<?>> messageConverters = restTemplate.getMessageConverters();
    for (int i = 0; i < messageConverters.size() -1 ; i++) {
        if (messageConverters.get(i) instanceof MappingJackson2XmlHttpMessageConverter) {
            Collections.swap(messageConverters, i,messageConverters.size() - 1);
        }
    }

    restTemplate.setMessageConverters(messageConverters);

    // add interceptors if necessary
    restTemplate.setInterceptors(Collections.singletonList(catalogInterceptior()));
    return restTemplate;
}

这篇关于如何在添加了jackson-dataformat-xml的情况下将默认MessageConverter设置为JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆