本文整理匯總了Java中org.springframework.http.converter.StringHttpMessageConverter.setSupportedMediaTypes方法的典型用法代碼示例。如果您正苦於以下問題:Java StringHttpMessageConverter.setSupportedMediaTypes方法的具體用法?Java StringHttpMessageConverter.setSupportedMediaTypes怎麽用?Java StringHttpMessageConverter.setSupportedMediaTypes使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.springframework.http.converter.StringHttpMessageConverter
的用法示例。
在下文中一共展示了StringHttpMessageConverter.setSupportedMediaTypes方法的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: verifyNoMarkupError
import org.springframework.http.converter.StringHttpMessageConverter; //導入方法依賴的package包/類
/**
* Verifies that the HTML is well formed.
*
* @param markup The HTML markup to be tested.
*/
public static void verifyNoMarkupError(String markup) {
// List<MediaType> supportedMediaTypes = new ArrayList<>();
List<MediaType> supportedMediaTypes = new ArrayList<MediaType>();
supportedMediaTypes.add(new MediaType("text", "html", Charset.forName("UTF-8")));
StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter();
stringHttpMessageConverter.setSupportedMediaTypes(supportedMediaTypes);
// List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
messageConverters.add(stringHttpMessageConverter);
RestTemplate restTemplate = new RestTemplate();
restTemplate.setMessageConverters(messageConverters);
String result = restTemplate.postForObject(URL, markup, String.class);
System.out.println("result: " + result);
assertTrue("The document is not valid HTML5: " + markup, result.contains("The document is valid HTML5"));
}
示例2: configureMessageConverters
import org.springframework.http.converter.StringHttpMessageConverter; //導入方法依賴的package包/類
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
ByteArrayHttpMessageConverter byteArrayHttpMessageConverter = new ByteArrayHttpMessageConverter();
StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter();
stringHttpMessageConverter.setDefaultCharset(Charset.forName("UTF-8"));
stringHttpMessageConverter.setSupportedMediaTypes(Collections.singletonList(MediaType.ALL));
//避免IE執行AJAX時,返回JSON出現下載文件
MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
mappingJackson2HttpMessageConverter.setDefaultCharset(Charset.forName("UTF-8"));
mappingJackson2HttpMessageConverter.setSupportedMediaTypes(asList(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML));
converters.add(byteArrayHttpMessageConverter);
converters.add(stringHttpMessageConverter);
converters.add(mappingJackson2HttpMessageConverter);
}
示例3: getFormPartsMessageConverters
import org.springframework.http.converter.StringHttpMessageConverter; //導入方法依賴的package包/類
private List<HttpMessageConverter<?>> getFormPartsMessageConverters() {
List<HttpMessageConverter<?>> partConverters = new ArrayList<HttpMessageConverter<?>>();
StringHttpMessageConverter stringConverter = new StringHttpMessageConverter();
stringConverter.setSupportedMediaTypes(Collections.singletonList(JsonUtil.JSON_MEDIA_TYPE));
stringConverter.setWriteAcceptCharset(false);
partConverters.add(stringConverter);
partConverters.add(new ResourceHttpMessageConverter());
partConverters.add(new UploadApplicationPayloadHttpMessageConverter());
return partConverters;
}
示例4: setStringConverterMediaTypes
import org.springframework.http.converter.StringHttpMessageConverter; //導入方法依賴的package包/類
private void setStringConverterMediaTypes(HttpMessageConverter<?> x) {
StringHttpMessageConverter converter = (StringHttpMessageConverter) x;
List<MediaType> mediaTypes = Arrays.asList(
new MediaType("text", "plain", StandardCharsets.UTF_8),
MediaType.ALL);
converter.setSupportedMediaTypes(mediaTypes);
}
示例5: initStringHttpMessageConverter
import org.springframework.http.converter.StringHttpMessageConverter; //導入方法依賴的package包/類
private StringHttpMessageConverter initStringHttpMessageConverter() {
StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
List<MediaType> supportedMediaTypes = new ArrayList<MediaType>();
supportedMediaTypes.add(new MediaType("text", "plain", Charset.defaultCharset()));
supportedMediaTypes.add(new MediaType("application", "json", Charset.defaultCharset()));
supportedMediaTypes.add(new MediaType("text", "html", Charset.defaultCharset()));
stringHttpMessageConverter.setSupportedMediaTypes(supportedMediaTypes);
return stringHttpMessageConverter;
}
示例6: responseBodyNoAcceptableMediaType
import org.springframework.http.converter.StringHttpMessageConverter; //導入方法依賴的package包/類
@Test
public void responseBodyNoAcceptableMediaType() throws ServletException, IOException {
@SuppressWarnings("serial") DispatcherServlet servlet = new DispatcherServlet() {
@Override
protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent) {
GenericWebApplicationContext wac = new GenericWebApplicationContext();
wac.registerBeanDefinition("controller", new RootBeanDefinition(RequestResponseBodyController.class));
RootBeanDefinition converterDef = new RootBeanDefinition(StringHttpMessageConverter.class);
converterDef.getPropertyValues().add("supportedMediaTypes", new MediaType("text", "plain"));
RootBeanDefinition adapterDef = new RootBeanDefinition(AnnotationMethodHandlerAdapter.class);
StringHttpMessageConverter converter = new StringHttpMessageConverter();
converter.setSupportedMediaTypes(Collections.singletonList(new MediaType("text", "plain")));
adapterDef.getPropertyValues().add("messageConverters", converter);
wac.registerBeanDefinition("handlerAdapter", adapterDef);
wac.refresh();
return wac;
}
};
servlet.init(new MockServletConfig());
MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/something");
String requestBody = "Hello World";
request.setContent(requestBody.getBytes("UTF-8"));
request.addHeader("Content-Type", "text/plain; charset=utf-8");
request.addHeader("Accept", "application/pdf, application/msword");
MockHttpServletResponse response = new MockHttpServletResponse();
servlet.service(request, response);
assertEquals(406, response.getStatus());
}
示例7: makeRestTemplateWithCustomObjectMapper
import org.springframework.http.converter.StringHttpMessageConverter; //導入方法依賴的package包/類
/**
* Gets a customized {@link MappingJackson2HttpMessageConverter} to process
* payload from TMS Rest API.
*
* @return
*/
protected void makeRestTemplateWithCustomObjectMapper(RestTemplate restTemplate) {
logger.debug("Getting new rest template");
for (HttpMessageConverter<?> httpMessageConverter : restTemplate.getMessageConverters()) {
if (httpMessageConverter instanceof MappingJackson2HttpMessageConverter) {
MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = (MappingJackson2HttpMessageConverter) httpMessageConverter;
// Bug with application/x-spring-data-compact+json: https://jira.spring.io/browse/DATAREST-404
logger.debug("Setting supported media type to just JSON. The Accept header will be updated accordingly to application/json");
mappingJackson2HttpMessageConverter.setSupportedMediaTypes(Arrays.asList(MediaType.APPLICATION_JSON));
// This helps with joda deserialize of joda date time string
logger.debug("Creating custom jackson2 objectmapper with serialization inclusion changes");
Jackson2ObjectMapperFactoryBean jackson2ObjectMapperFactoryBean = new Jackson2ObjectMapperFactoryBean();
jackson2ObjectMapperFactoryBean.setSerializationInclusion(JsonInclude.Include.NON_NULL);
jackson2ObjectMapperFactoryBean.afterPropertiesSet();
ObjectMapper objectMapper = jackson2ObjectMapperFactoryBean.getObject();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mappingJackson2HttpMessageConverter.setObjectMapper(objectMapper);
}
// The default encoding is set to ISO-8559-1 for String type, which is why we have to override it here
// For more info: https://jira.spring.io/browse/SPR-9099
if (httpMessageConverter instanceof StringHttpMessageConverter) {
StringHttpMessageConverter stringHttpMessageConverter = (StringHttpMessageConverter) httpMessageConverter;
stringHttpMessageConverter.setSupportedMediaTypes(Arrays.asList(
MediaType.parseMediaType("text/plain;charset=UTF-8"),
MediaType.ALL)
);
}
}
}
示例8: configureMessageConverters
import org.springframework.http.converter.StringHttpMessageConverter; //導入方法依賴的package包/類
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
StringHttpMessageConverter stringConverter = new StringHttpMessageConverter();
stringConverter.setSupportedMediaTypes(Arrays.asList(
new MediaType("application", "rdf", UTF8)));
converters.add(stringConverter);
}
示例9: getFormPartsMessageConverters
import org.springframework.http.converter.StringHttpMessageConverter; //導入方法依賴的package包/類
private List<HttpMessageConverter<?>> getFormPartsMessageConverters() {
List<HttpMessageConverter<?>> partConverters = new ArrayList<HttpMessageConverter<?>>();
StringHttpMessageConverter stringConverter = new StringHttpMessageConverter();
stringConverter.setSupportedMediaTypes(Collections.singletonList(JsonUtil.JSON_MEDIA_TYPE));
stringConverter.setWriteAcceptCharset(false);
partConverters.add(stringConverter);
partConverters.add(new ResourceHttpMessageConverter());
partConverters.add(new UploadApplicationPayloadHttpMessageConverter());
return partConverters;
}
示例10: createRestTemplate
import org.springframework.http.converter.StringHttpMessageConverter; //導入方法依賴的package包/類
@Override
protected RestTemplate createRestTemplate() {
ClientHttpRequestFactory requestFactory = ClientHttpRequestFactorySelector.getRequestFactory();
RestTemplate restTemplate = new RestTemplate(requestFactory);
List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>(2);
StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
stringHttpMessageConverter.setSupportedMediaTypes(Arrays.asList(MediaType.APPLICATION_FORM_URLENCODED));
converters.add(stringHttpMessageConverter);
restTemplate.setMessageConverters(converters);
return restTemplate;
}
示例11: configureMessageConverters
import org.springframework.http.converter.StringHttpMessageConverter; //導入方法依賴的package包/類
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(jacksonMessageConverter());
StringHttpMessageConverter converter = new StringHttpMessageConverter();
converter.setSupportedMediaTypes(Collections.singletonList(MediaType.ALL));
converters.add(converter);
}
示例12: stringMessageConverter
import org.springframework.http.converter.StringHttpMessageConverter; //導入方法依賴的package包/類
@Bean
public StringHttpMessageConverter stringMessageConverter() {
StringHttpMessageConverter messageConverter = new StringHttpMessageConverter();
messageConverter.setDefaultCharset(Charset.forName("UTF-8"));
messageConverter.setWriteAcceptCharset(false);
List<MediaType> mediaTypes = new ArrayList<>();
mediaTypes.add(MediaType.TEXT_PLAIN);
messageConverter.setSupportedMediaTypes(mediaTypes);
return messageConverter;
}
示例13: configureMessageConverters
import org.springframework.http.converter.StringHttpMessageConverter; //導入方法依賴的package包/類
/**
* Ensure that we always return UTF-8 when returning text/plain and text/html.
*/
@Override
public void configureMessageConverters(final List<HttpMessageConverter<?>> converters) {
final StringHttpMessageConverter stringConverter = new StringHttpMessageConverter(StandardCharsets.UTF_8);
stringConverter.setSupportedMediaTypes(Arrays.asList(new MediaType("text", "plain", StandardCharsets.UTF_8),
new MediaType("text", "html", StandardCharsets.UTF_8)));
converters.add(stringConverter);
addDefaultHttpMessageConverters(converters);
}
示例14: configureMessageConverters
import org.springframework.http.converter.StringHttpMessageConverter; //導入方法依賴的package包/類
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
StringHttpMessageConverter stringConverter = new StringHttpMessageConverter();
stringConverter.setSupportedMediaTypes(Arrays.asList(new MediaType("text", "plain", Charset.forName("UTF-8"))));
converters.add(stringConverter);
}