springboot針對controller層返回對象設置全局返回json格式


直接貼代碼:

import com.disney.wdpro.service.ecoupon.redemption.service.controller.interceptor.HttpStatusInterceptor;
import com.disney.wdpro.service.ecoupon.redemption.service.controller.interceptor.OpenapiInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class ConfigInterceptor implements WebMvcConfigurer {

    @Bean("httpStatusInterceptor")
    public HttpStatusInterceptor getHttpStatusInterceptor() {
        return new HttpStatusInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(getHttpStatusInterceptor()).addPathPatterns("/**");
    }

    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        configurer.ignoreAcceptHeader(true)  // 這里一定要設置為true,才能覆蓋原來的默認的xml format
                .defaultContentType(MediaType.APPLICATION_JSON);
    }
}

 

 

end.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM