org.springframework.http.converter.ResourceHttpMessageConverter Java Examples

The following examples show how to use org.springframework.http.converter.ResourceHttpMessageConverter. 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: RequestPartIntegrationTests.java    From spring-analysis-note with MIT License 7 votes vote down vote up
@Before
public void setup() {
	ByteArrayHttpMessageConverter emptyBodyConverter = new ByteArrayHttpMessageConverter();
	emptyBodyConverter.setSupportedMediaTypes(Collections.singletonList(MediaType.APPLICATION_JSON));

	List<HttpMessageConverter<?>> converters = new ArrayList<>(3);
	converters.add(emptyBodyConverter);
	converters.add(new ByteArrayHttpMessageConverter());
	converters.add(new ResourceHttpMessageConverter());
	converters.add(new MappingJackson2HttpMessageConverter());

	AllEncompassingFormHttpMessageConverter converter = new AllEncompassingFormHttpMessageConverter();
	converter.setPartConverters(converters);

	restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactory());
	restTemplate.setMessageConverters(Collections.singletonList(converter));
}
 
Example #2
Source File: ResourceHttpRequestHandler.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void afterPropertiesSet() throws Exception {
	if (logger.isWarnEnabled() && CollectionUtils.isEmpty(this.locations)) {
		logger.warn("Locations list is empty. No resources will be served unless a " +
				"custom ResourceResolver is configured as an alternative to PathResourceResolver.");
	}

	if (this.resourceResolvers.isEmpty()) {
		this.resourceResolvers.add(new PathResourceResolver());
	}
	initAllowedLocations();

	if (this.resourceHttpMessageConverter == null) {
		this.resourceHttpMessageConverter = new ResourceHttpMessageConverter();
	}
	if (this.resourceRegionHttpMessageConverter == null) {
		this.resourceRegionHttpMessageConverter = new ResourceRegionHttpMessageConverter();
	}

	this.contentNegotiationStrategy = initContentNegotiationStrategy();
}
 
Example #3
Source File: RequestPartIntegrationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() {
	ByteArrayHttpMessageConverter emptyBodyConverter = new ByteArrayHttpMessageConverter();
	emptyBodyConverter.setSupportedMediaTypes(Collections.singletonList(MediaType.APPLICATION_JSON));

	List<HttpMessageConverter<?>> converters = new ArrayList<>(3);
	converters.add(emptyBodyConverter);
	converters.add(new ByteArrayHttpMessageConverter());
	converters.add(new ResourceHttpMessageConverter());
	converters.add(new MappingJackson2HttpMessageConverter());

	AllEncompassingFormHttpMessageConverter converter = new AllEncompassingFormHttpMessageConverter();
	converter.setPartConverters(converters);

	restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactory());
	restTemplate.setMessageConverters(Collections.singletonList(converter));
}
 
Example #4
Source File: RequestResponseBodyMethodProcessorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void handleReturnValueImage() throws Exception {
	this.servletRequest.addHeader("Accept", "*/*");

	Method method = getClass().getDeclaredMethod("getImage");
	MethodParameter returnType = new MethodParameter(method, -1);

	List<HttpMessageConverter<?>> converters = new ArrayList<>();
	converters.add(new ResourceHttpMessageConverter());
	RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters);

	ClassPathResource resource = new ClassPathResource("logo.jpg", getClass());
	processor.writeWithMessageConverters(resource, returnType, this.request);

	assertEquals("image/jpeg", this.servletResponse.getHeader("Content-Type"));
}
 
Example #5
Source File: AnnotationDrivenBeanDefinitionParserTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@SuppressWarnings("unchecked")
private void verifyMessageConverters(Object bean, boolean hasDefaultRegistrations) {
	assertNotNull(bean);
	Object value = new DirectFieldAccessor(bean).getPropertyValue("messageConverters");
	assertNotNull(value);
	assertTrue(value instanceof List);
	List<HttpMessageConverter<?>> converters = (List<HttpMessageConverter<?>>) value;
	if (hasDefaultRegistrations) {
		assertTrue("Default and custom converter expected", converters.size() > 2);
	}
	else {
		assertTrue("Only custom converters expected", converters.size() == 2);
	}
	assertTrue(converters.get(0) instanceof StringHttpMessageConverter);
	assertTrue(converters.get(1) instanceof ResourceHttpMessageConverter);
}
 
Example #6
Source File: WebConfiguration.java    From java-platform with Apache License 2.0 6 votes vote down vote up
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
	StringHttpMessageConverter stringConverter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
	stringConverter.setWriteAcceptCharset(false);

	converters.add(new ByteArrayHttpMessageConverter());
	converters.add(stringConverter);
	converters.add(new ResourceHttpMessageConverter());
	converters.add(new SourceHttpMessageConverter<Source>());
	converters.add(new AllEncompassingFormHttpMessageConverter());
}
 
Example #7
Source File: RequestPartIntegrationTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Before
public void setup() {
	ByteArrayHttpMessageConverter emptyBodyConverter = new ByteArrayHttpMessageConverter();
	emptyBodyConverter.setSupportedMediaTypes(Collections.singletonList(MediaType.APPLICATION_JSON));

	List<HttpMessageConverter<?>> converters = new ArrayList<>(3);
	converters.add(emptyBodyConverter);
	converters.add(new ByteArrayHttpMessageConverter());
	converters.add(new ResourceHttpMessageConverter());
	converters.add(new MappingJackson2HttpMessageConverter());

	AllEncompassingFormHttpMessageConverter converter = new AllEncompassingFormHttpMessageConverter();
	converter.setPartConverters(converters);

	restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactory());
	restTemplate.setMessageConverters(Collections.singletonList(converter));
}
 
Example #8
Source File: RequestResponseBodyMethodProcessorTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void handleReturnValueImage() throws Exception {
	this.servletRequest.addHeader("Accept", "*/*");

	Method method = getClass().getDeclaredMethod("getImage");
	MethodParameter returnType = new MethodParameter(method, -1);

	List<HttpMessageConverter<?>> converters = new ArrayList<>();
	converters.add(new ResourceHttpMessageConverter());
	RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters);

	ClassPathResource resource = new ClassPathResource("logo.jpg", getClass());
	processor.writeWithMessageConverters(resource, returnType, this.request);

	assertEquals("image/jpeg", this.servletResponse.getHeader("Content-Type"));
}
 
Example #9
Source File: AnnotationDrivenBeanDefinitionParserTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@SuppressWarnings("unchecked")
private void verifyMessageConverters(Object bean, boolean hasDefaultRegistrations) {
	assertNotNull(bean);
	Object value = new DirectFieldAccessor(bean).getPropertyValue("messageConverters");
	assertNotNull(value);
	assertTrue(value instanceof List);
	List<HttpMessageConverter<?>> converters = (List<HttpMessageConverter<?>>) value;
	if (hasDefaultRegistrations) {
		assertTrue("Default and custom converter expected", converters.size() > 2);
	}
	else {
		assertTrue("Only custom converters expected", converters.size() == 2);
	}
	assertTrue(converters.get(0) instanceof StringHttpMessageConverter);
	assertTrue(converters.get(1) instanceof ResourceHttpMessageConverter);
}
 
Example #10
Source File: MessageConverterConfig.java    From sanshanblog with Apache License 2.0 5 votes vote down vote up
@Bean
public ResourceHttpMessageConverter  resourceHttpMessageConverter(){
    ResourceHttpMessageConverter res = new ResourceHttpMessageConverter();
    List<MediaType> mediaTypes = new LinkedList<MediaType>();
    mediaTypes.add(MediaType.APPLICATION_JSON);
    mediaTypes.add(MediaType.APPLICATION_OCTET_STREAM);
    mediaTypes.add(MediaType.IMAGE_JPEG);
    mediaTypes.add(MediaType.IMAGE_PNG);
    mediaTypes.add(MediaType.IMAGE_GIF);
    mediaTypes.add(MediaType.TEXT_PLAIN);
    res.setSupportedMediaTypes(mediaTypes);
     return res;
}
 
Example #11
Source File: MolgenisWebAppConfig.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
  converters.add(gsonHttpMessageConverter);
  converters.add(new BufferedImageHttpMessageConverter());
  converters.add(new CsvHttpMessageConverter());
  converters.add(new ResourceHttpMessageConverter());
  converters.add(new StringHttpMessageConverter());
  converters.add(rdfConverter);
}
 
Example #12
Source File: RequestResponseBodyMethodProcessorTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void handleReturnValueImage() throws Exception {
	this.servletRequest.addHeader("Accept", "*/*");

	Method method = getClass().getDeclaredMethod("getImage");
	MethodParameter returnType = new MethodParameter(method, -1);

	List<HttpMessageConverter<?>> converters = Collections.singletonList(new ResourceHttpMessageConverter());
	RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters);

	ClassPathResource resource = new ClassPathResource("logo.jpg", getClass());
	processor.writeWithMessageConverters(resource, returnType, this.webRequest);

	assertEquals("image/jpeg", this.servletResponse.getHeader("Content-Type"));
}
 
Example #13
Source File: AnnotationDrivenBeanDefinitionParserTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private void verifyMessageConverters(Object bean, boolean hasDefaultRegistrations) {
	assertNotNull(bean);
	Object value = new DirectFieldAccessor(bean).getPropertyValue("messageConverters");
	assertNotNull(value);
	assertTrue(value instanceof List);
	List<HttpMessageConverter<?>> converters = (List<HttpMessageConverter<?>>) value;
	if (hasDefaultRegistrations) {
		assertTrue("Default converters are registered in addition to custom ones", converters.size() > 2);
	} else {
		assertTrue("Default converters should not be registered", converters.size() == 2);
	}
	assertTrue(converters.get(0) instanceof StringHttpMessageConverter);
	assertTrue(converters.get(1) instanceof ResourceHttpMessageConverter);
}
 
Example #14
Source File: RestTemplate.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Create a new instance of the {@link RestTemplate} using default settings.
 * Default {@link HttpMessageConverter}s are initialized.
 */
public RestTemplate() {
	this.messageConverters.add(new ByteArrayHttpMessageConverter());
	this.messageConverters.add(new StringHttpMessageConverter());
	this.messageConverters.add(new ResourceHttpMessageConverter());
	this.messageConverters.add(new SourceHttpMessageConverter<Source>());
	this.messageConverters.add(new AllEncompassingFormHttpMessageConverter());

	if (romePresent) {
		this.messageConverters.add(new AtomFeedHttpMessageConverter());
		this.messageConverters.add(new RssChannelHttpMessageConverter());
	}

	if (jackson2XmlPresent) {
		this.messageConverters.add(new MappingJackson2XmlHttpMessageConverter());
	}
	else if (jaxb2Present) {
		this.messageConverters.add(new Jaxb2RootElementHttpMessageConverter());
	}

	if (jackson2Present) {
		this.messageConverters.add(new MappingJackson2HttpMessageConverter());
	}
	else if (gsonPresent) {
		this.messageConverters.add(new GsonHttpMessageConverter());
	}
}
 
Example #15
Source File: WebConfig.java    From WebIDE-Backend with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    converters.add(new ResourceHttpMessageConverter());

    GsonHttpMessageConverter converter = new GsonHttpMessageConverter();

    converter.setGson(gson());
    converters.add(converter);
}
 
Example #16
Source File: WebConfiguration.java    From spring-cloud-dataflow with Apache License 2.0 5 votes vote down vote up
@Bean
public HttpMessageConverters messageConverters(ObjectMapper objectMapper) {
	return new HttpMessageConverters(
			// Prevent default converters
			false,
			Arrays.<HttpMessageConverter<?>>asList(new MappingJackson2HttpMessageConverter(objectMapper),
					new ResourceHttpMessageConverter()));
}
 
Example #17
Source File: FormHttpMessageConverter.java    From onetwo with Apache License 2.0 5 votes vote down vote up
public FormHttpMessageConverter() {
	this.supportedMediaTypes.add(MediaType.APPLICATION_FORM_URLENCODED);
	this.supportedMediaTypes.add(MediaType.MULTIPART_FORM_DATA);

	StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter();
	stringHttpMessageConverter.setWriteAcceptCharset(false);  // see SPR-7316

	this.partConverters.add(new ByteArrayHttpMessageConverter());
	this.partConverters.add(stringHttpMessageConverter);
	this.partConverters.add(new ResourceHttpMessageConverter());

	applyDefaultCharset();
}
 
Example #18
Source File: WebConfig.java    From nakadi with MIT License 5 votes vote down vote up
@Override
protected void configureMessageConverters(final List<HttpMessageConverter<?>> converters) {
    final StringHttpMessageConverter stringConverter = new StringHttpMessageConverter();
    stringConverter.setWriteAcceptCharset(false);

    converters.add(new ByteArrayHttpMessageConverter());
    converters.add(stringConverter);
    converters.add(new ResourceHttpMessageConverter());
    converters.add(new SourceHttpMessageConverter<>());

    converters.add(mappingJackson2HttpMessageConverter());
    super.configureMessageConverters(converters);
}
 
Example #19
Source File: NetServiceImpl.java    From onboard with Apache License 2.0 5 votes vote down vote up
@PostConstruct
public void init() {
    restTemplate = new RestTemplate();

    restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
    restTemplate.getMessageConverters().add(new FormHttpMessageConverter());
    restTemplate.getMessageConverters().add(new ResourceHttpMessageConverter());
    requestConfig = RequestConfig.custom().setSocketTimeout(TIME_OUT).setConnectTimeout(TIME_OUT).build();
}
 
Example #20
Source File: RestTemplateFactory.java    From sdk-rest with MIT License 5 votes vote down vote up
private static RestTemplate createRestTemplate() {
	List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
	messageConverters.add(new ByteArrayHttpMessageConverter());
	messageConverters.add(new StringHttpMessageConverter(Charset.forName("UTF-8")));
	messageConverters.add(new ResourceHttpMessageConverter());
	messageConverters.add(new SourceHttpMessageConverter<Source>());
	messageConverters.add(new AllEncompassingFormHttpMessageConverter());
	messageConverters.add(new Jaxb2RootElementHttpMessageConverter());
	messageConverters.add(new MappingJackson2HttpMessageConverter());

	return new RestTemplate(messageConverters);
}
 
Example #21
Source File: RestUtil.java    From XPagesExtensionLibrary with Apache License 2.0 5 votes vote down vote up
private List<HttpMessageConverter<?>> getHttpMessageConverters() {
	List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
	messageConverters.add(new ByteArrayHttpMessageConverter());
	messageConverters.add(new StringHttpMessageConverter());
	messageConverters.add(new ResourceHttpMessageConverter());
	messageConverters.add(new UploadApplicationPayloadHttpMessageConverter());
	messageConverters.add(getFormHttpMessageConverter());
	messageConverters.add(new MappingJacksonHttpMessageConverter());
	return messageConverters;
}
 
Example #22
Source File: RestUtil.java    From XPagesExtensionLibrary with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #23
Source File: HttpClientConfig.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
private List<HttpMessageConverter<?>> converters() {
  List<HttpMessageConverter<?>> result = Lists.newArrayList();
  result.add(new ByteArrayHttpMessageConverter());
  result.add(new StringHttpMessageConverter());
  result.add(new ResourceHttpMessageConverter());
  result.add(new SourceHttpMessageConverter<>());
  result.add(new AllEncompassingFormHttpMessageConverter());
  result.add(new Jaxb2RootElementHttpMessageConverter());
  result.add(gsonHttpMessageConverter);
  return result;
}
 
Example #24
Source File: DefaultAuthServiceImpl.java    From SENS with GNU General Public License v3.0 5 votes vote down vote up
public static RestTemplate getRestTemplate() {// 手动添加
    SimpleClientHttpRequestFactory requestFactory=new SimpleClientHttpRequestFactory();
    requestFactory.setReadTimeout(120000);
    List<HttpMessageConverter<?>> messageConverters = new LinkedList<>();
    messageConverters.add(new ByteArrayHttpMessageConverter());
    messageConverters.add(new StringHttpMessageConverter(StandardCharsets.UTF_8));
    messageConverters.add(new ResourceHttpMessageConverter());
    messageConverters.add(new SourceHttpMessageConverter<Source>());
    messageConverters.add(new AllEncompassingFormHttpMessageConverter());
    messageConverters.add(new MappingJackson2HttpMessageConverter());
    RestTemplate restTemplate=new RestTemplate(messageConverters);
    restTemplate.setRequestFactory(requestFactory);
    return restTemplate;
}
 
Example #25
Source File: RestTemplate.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a new instance of the {@link RestTemplate} using default settings.
 * Default {@link HttpMessageConverter}s are initialized.
 */
public RestTemplate() {
	this.messageConverters.add(new ByteArrayHttpMessageConverter());
	this.messageConverters.add(new StringHttpMessageConverter());
	this.messageConverters.add(new ResourceHttpMessageConverter());
	this.messageConverters.add(new SourceHttpMessageConverter<Source>());
	this.messageConverters.add(new AllEncompassingFormHttpMessageConverter());

	if (romePresent) {
		this.messageConverters.add(new AtomFeedHttpMessageConverter());
		this.messageConverters.add(new RssChannelHttpMessageConverter());
	}

	if (jackson2XmlPresent) {
		this.messageConverters.add(new MappingJackson2XmlHttpMessageConverter());
	}
	else if (jaxb2Present) {
		this.messageConverters.add(new Jaxb2RootElementHttpMessageConverter());
	}

	if (jackson2Present) {
		this.messageConverters.add(new MappingJackson2HttpMessageConverter());
	}
	else if (gsonPresent) {
		this.messageConverters.add(new GsonHttpMessageConverter());
	}
}
 
Example #26
Source File: ResourceHttpRequestHandler.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public void afterPropertiesSet() throws Exception {
	resolveResourceLocations();

	if (logger.isWarnEnabled() && CollectionUtils.isEmpty(this.locations)) {
		logger.warn("Locations list is empty. No resources will be served unless a " +
				"custom ResourceResolver is configured as an alternative to PathResourceResolver.");
	}

	if (this.resourceResolvers.isEmpty()) {
		this.resourceResolvers.add(new PathResourceResolver());
	}

	initAllowedLocations();

	// Initialize immutable resolver and transformer chains
	this.resolverChain = new DefaultResourceResolverChain(this.resourceResolvers);
	this.transformerChain = new DefaultResourceTransformerChain(this.resolverChain, this.resourceTransformers);

	if (this.resourceHttpMessageConverter == null) {
		this.resourceHttpMessageConverter = new ResourceHttpMessageConverter();
	}
	if (this.resourceRegionHttpMessageConverter == null) {
		this.resourceRegionHttpMessageConverter = new ResourceRegionHttpMessageConverter();
	}

	this.contentNegotiationStrategy = initContentNegotiationStrategy();
}
 
Example #27
Source File: ResourceHandlerFunctionTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Before
public void createContext() {
	this.messageConverter = new ResourceHttpMessageConverter();
	this.context = new ServerResponse.Context() {
		@Override
		public List<HttpMessageConverter<?>> messageConverters() {
			return Collections.singletonList(messageConverter);
		}

	};
}
 
Example #28
Source File: RestUtil.java    From cf-java-client-sap with Apache License 2.0 5 votes vote down vote up
private List<HttpMessageConverter<?>> getHttpMessageConverters() {
    List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
    messageConverters.add(new ByteArrayHttpMessageConverter());
    messageConverters.add(new StringHttpMessageConverter());
    messageConverters.add(new ResourceHttpMessageConverter());
    messageConverters.add(new MappingJackson2HttpMessageConverter());
    return messageConverters;
}
 
Example #29
Source File: TraderMainConfiguration.java    From java-trader with Apache License 2.0 5 votes vote down vote up
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    converters.add(new StringHttpMessageConverter());
    converters.add(new FormHttpMessageConverter());
    GsonHttpMessageConverter c = new GsonHttpMessageConverter();
    c.setGson(new GsonBuilder().disableHtmlEscaping().create());
    converters.add(c);
    converters.add(new ResourceHttpMessageConverter());
}
 
Example #30
Source File: TraderUMainConfiguration.java    From java-trader with Apache License 2.0 5 votes vote down vote up
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    converters.add(new StringHttpMessageConverter());
    converters.add(new FormHttpMessageConverter());
    GsonHttpMessageConverter c = new GsonHttpMessageConverter();
    c.setGson(new GsonBuilder().disableHtmlEscaping().create());
    converters.add(c);
    converters.add(new ResourceHttpMessageConverter());
}