简体   繁体   English

如何使用全局设置强制springboot返回Json响应

[英]How to force springboot return Json response with global settings

Here are problems.这里有问题。 They who are testing my application told me that when they trying to directly input the url into address to query a GET url by chrome.正在测试我的应用程序的他们告诉我,当他们尝试直接将 url 输入地址以通过 chrome 查询 GET url 时。 My api will always return a XML formatted response like this.我的 api 将始终返回像这样的 XML 格式的响应。 在此处输入图像描述

But all the response should be formatted in JSON string !但是所有的响应都应该格式化为 JSON 字符串! XML is unacceptable ! XML 是不可接受的!

I tried to analyze the source code and found request sending by chrome directly will bear attributes like this:我试图分析源代码,发现直接通过chrome发送的请求会带有这样的属性:

accept= text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng, / ;q=0.8,application/signed-exchange;v=b3;q=0.9接受= text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng, / ;q=0.8,application/signed-exchange;v=b3;q= 0.9
content-type=null内容类型=空

while any other application will send request with:而任何其他应用程序将发送请求:

accept= */* (or accept = application/json)接受= */*(或接受=应用程序/json)
content-type = application/json内容类型 = 应用程序/json

In this case, springboot will then response XML to the request send by chrome because of the missing content-type and specified accept properties.在这种情况下,由于缺少 content-type 和指定的 accept 属性,springboot 将响应 XML 到 chrome 发送的请求。

There is a way to fix this case by excluding jackson for XML in pom.有一种方法可以通过在 pom.xml 中排除 XML 的 jackson 来解决此问题。 But I don't think it is a graceful way.但我不认为这是一种优雅的方式。

So far I refused to fix this problem because I have told Front-end code colleague to send request with specified "application/json" attribute and no problem occured again.到目前为止,我拒绝修复这个问题,因为我已经告诉前端代码同事发送带有指定“application/json”属性的请求,并且没有再次出现问题。

But still, I think I have responsibilities to fix it and there should be ways more graceful to fix this problem.但是,我认为我有责任解决它,应该有更优雅的方法来解决这个问题。

There are too many api deployed in my application and there is no way to set a response type for every controller.我的应用程序中部署的 api 太多,无法为每个 controller 设置响应类型。 I'm looking for a global setting for this case.我正在为这种情况寻找一个全局设置。

I think there should be some way to act like RequestInterceptor to set headers of a request, but I found that impossible to directly modify the headers from those request in my gateway service.我认为应该有某种方式可以像 RequestInterceptor 那样设置请求的标头,但我发现在我的网关服务中直接修改这些请求的标头是不可能的。

Help me with this case please.请帮我处理这个案子。 Thanks a lot!非常感谢!

It sounds like you'll want to define a ContentNegotiationCongiurer.听起来您需要定义一个 ContentNegotiationCongiurer。 Here's a doc: https://www.baeldung.com/spring-mvc-content-negotiation-json-xml这是一个文档: https://www.baeldung.com/spring-mvc-content-negotiation-json-xml

The short answer is to add something like this to the Spring context简短的回答是将这样的内容添加到 Spring 上下文中

@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        configurer.defaultContentType(MediaType.APPLICATION_JSON);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM