Browse Source

Polishing

pull/1086/head
Juergen Hoeller 10 years ago
parent
commit
9ac661df82
  1. 2
      spring-web/src/main/java/org/springframework/http/converter/AbstractHttpMessageConverter.java
  2. 8
      spring-web/src/main/java/org/springframework/http/converter/ByteArrayHttpMessageConverter.java
  3. 4
      spring-web/src/main/java/org/springframework/http/converter/FormHttpMessageConverter.java
  4. 37
      spring-web/src/main/java/org/springframework/http/converter/ObjectToStringHttpMessageConverter.java

2
spring-web/src/main/java/org/springframework/http/converter/AbstractHttpMessageConverter.java

@ -200,7 +200,7 @@ public abstract class AbstractHttpMessageConverter<T> implements HttpMessageConv @@ -200,7 +200,7 @@ public abstract class AbstractHttpMessageConverter<T> implements HttpMessageConv
/**
* Add default headers to the output message.
* <p>This implementation delegates to {@link #getDefaultContentType(Object)} if a content
* type was not provided, calls {@link #getContentLength}, and sets the corresponding headers
* type was not provided, calls {@link #getContentLength}, and sets the corresponding headers.
* @since 4.2
*/
protected void addDefaultHeaders(HttpHeaders headers, T t, MediaType contentType) throws IOException{

8
spring-web/src/main/java/org/springframework/http/converter/ByteArrayHttpMessageConverter.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -32,15 +32,19 @@ import org.springframework.util.StreamUtils; @@ -32,15 +32,19 @@ import org.springframework.util.StreamUtils;
* overridden by setting the {@link #setSupportedMediaTypes supportedMediaTypes} property.
*
* @author Arjen Poutsma
* @author Juergen Hoeller
* @since 3.0
*/
public class ByteArrayHttpMessageConverter extends AbstractHttpMessageConverter<byte[]> {
/** Creates a new instance of the {@code ByteArrayHttpMessageConverter}. */
/**
* Create a new instance of the {@code ByteArrayHttpMessageConverter}.
*/
public ByteArrayHttpMessageConverter() {
super(new MediaType("application", "octet-stream"), MediaType.ALL);
}
@Override
public boolean supports(Class<?> clazz) {
return byte[].class == clazz;

4
spring-web/src/main/java/org/springframework/http/converter/FormHttpMessageConverter.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -255,7 +255,7 @@ public class FormHttpMessageConverter implements HttpMessageConverter<MultiValue @@ -255,7 +255,7 @@ public class FormHttpMessageConverter implements HttpMessageConverter<MultiValue
Charset charset;
if (contentType != null) {
outputMessage.getHeaders().setContentType(contentType);
charset = contentType.getCharSet() != null ? contentType.getCharSet() : this.charset;
charset = (contentType.getCharSet() != null ? contentType.getCharSet() : this.charset);
}
else {
outputMessage.getHeaders().setContentType(MediaType.APPLICATION_FORM_URLENCODED);

37
spring-web/src/main/java/org/springframework/http/converter/ObjectToStringHttpMessageConverter.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -29,11 +29,12 @@ import org.springframework.util.Assert; @@ -29,11 +29,12 @@ import org.springframework.util.Assert;
* An {@code HttpMessageConverter} that uses {@link StringHttpMessageConverter}
* for reading and writing content and a {@link ConversionService} for converting
* the String content to and from the target object type.
* <p>
* By default, this converter supports the media type {@code text/plain} only.
* This can be overridden by setting the
* {@link #setSupportedMediaTypes supportedMediaTypes} property.
* Example of usage:
*
* <p>By default, this converter supports the media type {@code text/plain} only.
* This can be overridden through the {@link #setSupportedMediaTypes supportedMediaTypes}
* property.
*
* <p>A usage example:
*
* <pre class="code">
* &lt;bean class="org.springframework.http.converter.ObjectToStringHttpMessageConverter">
@ -49,17 +50,15 @@ import org.springframework.util.Assert; @@ -49,17 +50,15 @@ import org.springframework.util.Assert;
*/
public class ObjectToStringHttpMessageConverter extends AbstractHttpMessageConverter<Object> {
private ConversionService conversionService;
private final ConversionService conversionService;
private StringHttpMessageConverter stringHttpMessageConverter;
private final StringHttpMessageConverter stringHttpMessageConverter;
/**
* A constructor accepting a {@code ConversionService} to use to convert the
* (String) message body to/from the target class type. This constructor
* uses {@link StringHttpMessageConverter#DEFAULT_CHARSET} as the default
* charset.
*
* (String) message body to/from the target class type. This constructor uses
* {@link StringHttpMessageConverter#DEFAULT_CHARSET} as the default charset.
* @param conversionService the conversion service
*/
public ObjectToStringHttpMessageConverter(ConversionService conversionService) {
@ -67,20 +66,19 @@ public class ObjectToStringHttpMessageConverter extends AbstractHttpMessageConve @@ -67,20 +66,19 @@ public class ObjectToStringHttpMessageConverter extends AbstractHttpMessageConve
}
/**
* A constructor accepting a {@code ConversionService} as well as a default
* charset.
*
* A constructor accepting a {@code ConversionService} as well as a default charset.
* @param conversionService the conversion service
* @param defaultCharset the default charset
*/
public ObjectToStringHttpMessageConverter(ConversionService conversionService, Charset defaultCharset) {
super(new MediaType("text", "plain", defaultCharset));
Assert.notNull(conversionService, "conversionService is required");
Assert.notNull(conversionService, "ConversionService is required");
this.conversionService = conversionService;
this.stringHttpMessageConverter = new StringHttpMessageConverter(defaultCharset);
}
/**
* Indicates whether the {@code Accept-Charset} should be written to any outgoing request.
* <p>Default is {@code true}.
@ -89,6 +87,7 @@ public class ObjectToStringHttpMessageConverter extends AbstractHttpMessageConve @@ -89,6 +87,7 @@ public class ObjectToStringHttpMessageConverter extends AbstractHttpMessageConve
this.stringHttpMessageConverter.setWriteAcceptCharset(writeAcceptCharset);
}
@Override
public boolean canRead(Class<?> clazz, MediaType mediaType) {
return this.conversionService.canConvert(String.class, clazz) && canRead(mediaType);
@ -106,15 +105,15 @@ public class ObjectToStringHttpMessageConverter extends AbstractHttpMessageConve @@ -106,15 +105,15 @@ public class ObjectToStringHttpMessageConverter extends AbstractHttpMessageConve
}
@Override
protected Object readInternal(Class<? extends Object> clazz, HttpInputMessage inputMessage) throws IOException {
protected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage) throws IOException {
String value = this.stringHttpMessageConverter.readInternal(String.class, inputMessage);
return this.conversionService.convert(value, clazz);
}
@Override
protected void writeInternal(Object obj, HttpOutputMessage outputMessage) throws IOException {
String s = this.conversionService.convert(obj, String.class);
this.stringHttpMessageConverter.writeInternal(s, outputMessage);
String value = this.conversionService.convert(obj, String.class);
this.stringHttpMessageConverter.writeInternal(value, outputMessage);
}
@Override

Loading…
Cancel
Save