org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer Java Examples

The following examples show how to use org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ConfigurableContentNegotiationManagerWebMvcConfigurerTest.java    From spring-webmvc-support with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testConfigureContentNegotiationOnDefaultValues() {

    WebMvcConfigurer webMvcConfigurer =
            new ConfigurableContentNegotiationManagerWebMvcConfigurer(new HashMap<String, String>());

    ContentNegotiationConfigurer configurer = new ContentNegotiationConfigurer(new MockServletContext());

    webMvcConfigurer.configureContentNegotiation(configurer);

    ContentNegotiationManagerFactoryBean factoryBean =
            FieldUtils.getFieldValue(configurer, "factory", ContentNegotiationManagerFactoryBean.class);

    Assert.assertTrue(FieldUtils.getFieldValue(factoryBean, "favorPathExtension", boolean.class));
    Assert.assertFalse(FieldUtils.getFieldValue(factoryBean, "favorParameter", boolean.class));
    Assert.assertFalse(FieldUtils.getFieldValue(factoryBean, "ignoreAcceptHeader", boolean.class));
    Assert.assertNull(FieldUtils.getFieldValue(factoryBean, "useJaf", Boolean.class));
    Assert.assertEquals("format", FieldUtils.getFieldValue(factoryBean, "parameterName", String.class));
    Assert.assertTrue(FieldUtils.getFieldValue(factoryBean, "mediaTypes", Map.class).isEmpty());
    Assert.assertNull(FieldUtils.getFieldValue(factoryBean, "defaultContentType", MediaType.class));

}
 
Example #2
Source File: WebMVCConfig.java    From spring-boot with Apache License 2.0 5 votes vote down vote up
/**
 * Spring 3.2 及以上版本自动开启检测URL后缀,设置Response content-type功能, 如果不手动关闭这个功能,当url后缀与accept头不一致时,
 * Response的content-type将会和request的accept不一致,导致报 406 错误
 * 例如返回类型为 JSON 的 @ResponseBody API, 必须将请求URL后缀改为.json,以便和 accept头(application/json)相匹配,否则返回 406 错误。
 */
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {

    configurer.favorPathExtension(false). //关闭URL后缀检测的方法如下
            favorParameter(true).
            mediaType("json", MediaType.APPLICATION_JSON).
            defaultContentType(MediaType.APPLICATION_JSON);//如果没有对应的后缀名,返回信息默认以 json 格式返回

}
 
Example #3
Source File: BootMvcConfigurerAdapter.java    From onetwo with Apache License 2.0 5 votes vote down vote up
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
	Properties mediaTypes = jfishBootConfig.getMvc().getMediaTypes();
	if (!CollectionUtils.isEmpty(mediaTypes)) {
		for (Entry<Object, Object> entry : mediaTypes.entrySet()) {
			String extension = ((String)entry.getKey()).toLowerCase(Locale.ENGLISH);
			configurer.mediaType(extension, MediaType.valueOf((String) entry.getValue()));
		}
	}
}
 
Example #4
Source File: KafDrop.java    From Kafdrop with Apache License 2.0 5 votes vote down vote up
@Bean
public WebMvcConfigurerAdapter webConfig()
{
   return new WebMvcConfigurerAdapter()
   {
      @Override
      public void configureContentNegotiation(ContentNegotiationConfigurer configurer)
      {
         super.configureContentNegotiation(configurer);
         configurer.favorPathExtension(false);
      }
   };
}
 
Example #5
Source File: DelegatingCatnapWebMvcConfiguration.java    From catnap with Apache License 2.0 5 votes vote down vote up
@Override
protected void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
    configurer.defaultContentType(MediaType.APPLICATION_JSON);
    configurer.favorPathExtension(true);
    configurer.ignoreAcceptHeader(false);
    configurer.mediaType("json", MediaType.APPLICATION_JSON);
    configurer.mediaType("jsonp", new MediaType("application", "x-javascript"));

    super.configureContentNegotiation(configurer);
}
 
Example #6
Source File: WidgetConfiguration.java    From catnap with Apache License 2.0 5 votes vote down vote up
@Override
protected void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
    configurer.defaultContentType(MediaType.APPLICATION_JSON);
    configurer.favorPathExtension(true);
    configurer.ignoreAcceptHeader(false);
    configurer.mediaType("json", MediaType.APPLICATION_JSON);
    configurer.mediaType("jsonp", new MediaType("application", "x-javascript"));

    super.configureContentNegotiation(configurer);
}
 
Example #7
Source File: WebMvcConfig.java    From sc-generator with Apache License 2.0 5 votes vote down vote up
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
    configurer.mediaType("adoc", MediaType.parseMediaType("text/asciidoc;charset=utf-8"))
            .mediaType("md", MediaType.parseMediaType("text/markdown;charset=utf-8"))
            .mediaType("html", MediaType.parseMediaType("text/html;charset=utf-8"))
            .mediaType("properties", MediaType.parseMediaType("text/properties;charset=utf-8"))
            .mediaType("yml", MediaType.parseMediaType("text/yaml;charset=utf-8"))
            .mediaType("sql", MediaType.parseMediaType(MediaType.TEXT_PLAIN_VALUE + ";charset=utf-8"))
            .mediaType("jdl", MediaType.parseMediaType(MediaType.TEXT_PLAIN_VALUE + ";charset=utf-8"))
            .mediaType("doc", MediaType.parseMediaType("application/msword"));
    super.configureContentNegotiation(configurer);
}
 
Example #8
Source File: WebMvcConfig.java    From konker-platform with Apache License 2.0 5 votes vote down vote up
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
    configurer.favorPathExtension(true)
        .favorParameter(false)
        .ignoreAcceptHeader(false)
        .useJaf(false)
        .defaultContentType(MediaType.APPLICATION_JSON)
        .mediaType("xml", MediaType.APPLICATION_XML)
        .mediaType("json", MediaType.APPLICATION_JSON);
}
 
Example #9
Source File: WebMVCConfig.java    From spring-boot with Apache License 2.0 5 votes vote down vote up
/**
 * Spring 3.2 及以上版本自动开启检测URL后缀,设置Response content-type功能, 如果不手动关闭这个功能,当url后缀与accept头不一致时,
 * Response的content-type将会和request的accept不一致,导致报 406 错误
 * 例如返回类型为 JSON 的 @ResponseBody API, 必须将请求URL后缀改为.json,以便和 accept头(application/json)相匹配,否则返回 406 错误。
 */
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {

    configurer.
            favorPathExtension(false). //关闭URL后缀检测
            favorParameter(true).
            parameterName("mediaType").
            ignoreAcceptHeader(true).
            useRegisteredExtensionsOnly(false).
            mediaType("xml", MediaType.APPLICATION_XML).
            mediaType("json", MediaType.APPLICATION_JSON).
            defaultContentType(MediaType.APPLICATION_JSON);//如果没有对应的后缀名,返回信息默认以 json 格式返回

}
 
Example #10
Source File: WebConfig.java    From tutorials with MIT License 5 votes vote down vote up
/**
 * Spring Boot allows configuring Content Negotiation using properties
 */
@Override
public void configureContentNegotiation(final ContentNegotiationConfigurer configurer) {
    configurer.favorPathExtension(true)
        .favorParameter(true)
        .parameterName("mediaType")
        .ignoreAcceptHeader(false)
        .useRegisteredExtensionsOnly(false)
        .defaultContentType(MediaType.APPLICATION_JSON)
        .mediaType("xml", MediaType.APPLICATION_XML)
        .mediaType("json", MediaType.APPLICATION_JSON);
}
 
Example #11
Source File: RestConfig.java    From kbear with Apache License 2.0 5 votes vote down vote up
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
    configurer.defaultContentType(MediaType.APPLICATION_JSON)
            .strategies(Arrays.asList(new ContentNegotiationStrategy() {
                @Override
                public List<MediaType> resolveMediaTypes(NativeWebRequest webRequest)
                        throws HttpMediaTypeNotAcceptableException {
                    MediaType mediaType = MediaType.APPLICATION_JSON;
                    String accept = webRequest.getHeader(HttpHeaders.ACCEPT);
                    if (accept == null)
                        return Arrays.asList(mediaType);

                    switch (accept) {
                        case APPLICATION_PROTOBUF_VALUE:
                            mediaType = ProtobufHttpMessageConverter.PROTOBUF;
                            break;
                        case MediaType.APPLICATION_JSON_VALUE:
                            mediaType = MediaType.APPLICATION_JSON;
                            break;
                        default:
                            mediaType = MediaType.APPLICATION_JSON;
                            break;
                    }

                    return Arrays.asList(mediaType);
                }
            }));
}
 
Example #12
Source File: WebConfig.java    From ifsc-rest-api with MIT License 5 votes vote down vote up
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
    configurer.favorPathExtension(false).
            favorParameter(true).
            parameterName("mediaType").
            ignoreAcceptHeader(true).
            useJaf(false).
            defaultContentType(MediaType.APPLICATION_JSON).
            mediaType("xml", MediaType.APPLICATION_XML).
            mediaType("json", MediaType.APPLICATION_JSON);
}
 
Example #13
Source File: MvcConfiguration.java    From SMSC with Apache License 2.0 4 votes vote down vote up
/**
 * Configure content negotiation options.
 */
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
    configurer.favorPathExtension(false);
}
 
Example #14
Source File: WebConfig.java    From spring4-sandbox with Apache License 2.0 4 votes vote down vote up
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
	configurer.favorParameter(false);
	configurer.favorPathExtension(false);
}
 
Example #15
Source File: WebConfig.java    From spring4-sandbox with Apache License 2.0 4 votes vote down vote up
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
    configurer.favorParameter(false);
    configurer.favorPathExtension(false);
}
 
Example #16
Source File: WebConfig.java    From spring4-sandbox with Apache License 2.0 4 votes vote down vote up
@Override
public void configureContentNegotiation(
		ContentNegotiationConfigurer configurer) {
	configurer.favorParameter(false);
	configurer.favorPathExtension(false);
}
 
Example #17
Source File: WebConfig.java    From spring4-sandbox with Apache License 2.0 4 votes vote down vote up
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
    configurer.favorParameter(false);
    configurer.favorPathExtension(false);
}
 
Example #18
Source File: MvcConfig.java    From tutorials with MIT License 4 votes vote down vote up
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
    configurer.defaultContentType(MediaType.APPLICATION_JSON);
}
 
Example #19
Source File: MvcConfig.java    From tutorials with MIT License 4 votes vote down vote up
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
    configurer.defaultContentType(MediaType.APPLICATION_JSON);
}
 
Example #20
Source File: WebConfig.java    From spring4-sandbox with Apache License 2.0 4 votes vote down vote up
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
	configurer.favorParameter(false);
	configurer.favorPathExtension(false);
}
 
Example #21
Source File: CatnapWebMvcConfigurerAdapter.java    From catnap with Apache License 2.0 4 votes vote down vote up
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
    configurer.mediaType("json", MediaType.APPLICATION_JSON);
    configurer.mediaType("jsonp", new MediaType("application", "x-javascript"));
}
 
Example #22
Source File: MolgenisWebAppConfig.java    From molgenis with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
  // Fix for https://github.com/molgenis/molgenis/issues/6575
  configurer.favorPathExtension(false);
}
 
Example #23
Source File: PresentationConfiguration.java    From hesperides with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void configureContentNegotiation(final ContentNegotiationConfigurer configurer) {
    configurer.favorPathExtension(false);
}
 
Example #24
Source File: DispatcherServletConfiguration.java    From flowable-engine with Apache License 2.0 4 votes vote down vote up
@Override
protected void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
    configurer.favorPathExtension(false);
}
 
Example #25
Source File: DispatcherServletConfiguration.java    From flowable-engine with Apache License 2.0 4 votes vote down vote up
@Override
protected void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
    configurer.favorPathExtension(false);
}
 
Example #26
Source File: DispatcherServletConfiguration.java    From flowable-engine with Apache License 2.0 4 votes vote down vote up
@Override
protected void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
    configurer.favorPathExtension(false);
}
 
Example #27
Source File: DispatcherServletConfiguration.java    From flowable-engine with Apache License 2.0 4 votes vote down vote up
@Override
protected void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
    configurer.favorPathExtension(false);
}
 
Example #28
Source File: DispatcherServletConfiguration.java    From flowable-engine with Apache License 2.0 4 votes vote down vote up
@Override
protected void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
    configurer.favorPathExtension(false);
}
 
Example #29
Source File: DispatcherServletConfiguration.java    From flowable-engine with Apache License 2.0 4 votes vote down vote up
@Override
protected void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
    configurer.favorPathExtension(false);
}
 
Example #30
Source File: DispatcherServletConfiguration.java    From flowable-engine with Apache License 2.0 4 votes vote down vote up
@Override
protected void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
    configurer.favorPathExtension(false);
}