通过配置文件的方式
package cn.edu.tju.config;
import cn.edu.tju.interceptor.MyInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
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.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
import org.springframework.web.servlet.view.ResourceBundleViewResolver;
@Configuration
@EnableWebMvc
public class MyWebConfig implements WebMvcConfigurer {
@Autowired
private MyInterceptor myInterceptor;
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer.favorParameter(true);
configurer.favorPathExtension(true);
//可不配置,默认未format
//configurer.parameterName("test");
configurer.mediaType("fgh", MediaType.APPLICATION_XML);
configurer.mediaType("mnp", MediaType.APPLICATION_JSON);
//默认添加
// configurer.mediaType("xml", MediaType.APPLICATION_XML);
// configurer.mediaType("json", MediaType.APPLICATION_JSON);
}
}
请求格式
http://localhost:9093/hi3?format=mnp