Polishing

This commit is contained in:
Juergen Hoeller 2014-05-27 18:29:51 +02:00
parent 22a38d4547
commit 8220832c4e
3 changed files with 40 additions and 47 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -25,13 +25,14 @@ import java.lang.annotation.Target;
* Declares that a field should be formatted as a number. * Declares that a field should be formatted as a number.
* Supports formatting by style or custom pattern string. * Supports formatting by style or custom pattern string.
* Can be applied to any JDK {@code java.lang.Number} type. * Can be applied to any JDK {@code java.lang.Number} type.
* <p> *
* For style-based formatting, set the {@link #style()} attribute to be the desired {@link Style}. * <p>For style-based formatting, set the {@link #style()} attribute to be the desired {@link Style}.
* For custom formatting, set the {@link #pattern()} attribute to be the number pattern, such as {@code #, ###.##}. * For custom formatting, set the {@link #pattern()} attribute to be the number pattern, such as {@code #, ###.##}.
* <p> *
* Each attribute is mutually exclusive, so only set one attribute per annotation instance (the one most convenient one for your formatting needs). * <p>Each attribute is mutually exclusive, so only set one attribute per annotation instance
* When the pattern attribute is specified, it takes precedence over the style attribute. * (the one most convenient one for your formatting needs). When the pattern attribute is specified,
* When no annotation attributes are specified, the default format applied is style-based with a style of {@link Style#NUMBER}. * it takes precedence over the style attribute. When no annotation attributes are specified,
* the default format applied is style-based with a style of {@link Style#NUMBER}.
* *
* @author Keith Donald * @author Keith Donald
* @since 3.0 * @since 3.0
@ -43,23 +44,23 @@ public @interface NumberFormat {
/** /**
* The style pattern to use to format the field. * The style pattern to use to format the field.
* Defaults to {@link Style#NUMBER} for general-purpose number formatter. * <p>Defaults to {@link Style#NUMBER} for general-purpose number formatter.
* Set this attribute when you wish to format your field in accordance with a common style other than the default style. * Set this attribute when you wish to format your field in accordance with a
* common style other than the default style.
*/ */
Style style() default Style.NUMBER; Style style() default Style.NUMBER;
/** /**
* The custom pattern to use to format the field. * The custom pattern to use to format the field.
* Defaults to empty String, indicating no custom pattern String has been specified. * <p>Defaults to empty String, indicating no custom pattern String has been specified.
* Set this attribute when you wish to format your field in accordance with a custom number pattern not represented by a style. * Set this attribute when you wish to format your field in accordance with a
* custom number pattern not represented by a style.
*/ */
String pattern() default ""; String pattern() default "";
/** /**
* Common number format styles. * Common number format styles.
* @author Keith Donald
* @since 3.0
*/ */
public enum Style { public enum Style {

View File

@ -19,32 +19,30 @@ package org.springframework.http.converter.json;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import org.apache.commons.codec.binary.Base64;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext; import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer; import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive; import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext; import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer; import com.google.gson.JsonSerializer;
import com.google.gson.TypeAdapter; import com.google.gson.TypeAdapter;
import org.apache.commons.codec.binary.Base64;
/** /**
* Custom Gson {@link TypeAdapter} for serialization or deserialization of * Custom Gson {@link TypeAdapter} for serialization and deserialization
* {@code byte[]}. By default Gson converts byte arrays to JSON arrays instead * of {@code byte[]} values to/from Base64-encoded Strings.
* of a Base64 encoded string. Use this type adapter with *
* {@link org.springframework.http.converter.json.GsonHttpMessageConverter * <p>By default, Gson converts byte arrays to JSON arrays. This type adapter
* GsonHttpMessageConverter} to read and write Base64 encoded byte arrays. * needs to be specifically registered to read/write Base64-encoded byte arrays.
* *
* @author Roy Clarkson * @author Roy Clarkson
* @since 4.1 * @since 4.1
* @see GsonBuilder#registerTypeHierarchyAdapter(Class, Object) * @see GsonBuilder#registerTypeHierarchyAdapter(Class, Object)
*/ */
final class GsonBase64ByteArrayJsonTypeAdapter implements JsonSerializer<byte[]>, JsonDeserializer<byte[]> { class GsonBase64ByteArrayJsonTypeAdapter implements JsonSerializer<byte[]>, JsonDeserializer<byte[]> {
public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8"); private static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
private final Base64 base64 = new Base64(); private final Base64 base64 = new Base64();
@ -56,7 +54,7 @@ final class GsonBase64ByteArrayJsonTypeAdapter implements JsonSerializer<byte[]>
} }
@Override @Override
public byte[] deserialize(JsonElement json, Type type, JsonDeserializationContext cxt) throws JsonParseException { public byte[] deserialize(JsonElement json, Type type, JsonDeserializationContext cxt) {
return this.base64.decode(json.getAsString().getBytes(DEFAULT_CHARSET)); return this.base64.decode(json.getAsString().getBytes(DEFAULT_CHARSET));
} }

View File

@ -23,6 +23,11 @@ import java.io.Reader;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import com.google.gson.Gson;
import com.google.gson.JsonIOException;
import com.google.gson.JsonParseException;
import com.google.gson.reflect.TypeToken;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpInputMessage; import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage; import org.springframework.http.HttpOutputMessage;
@ -33,28 +38,21 @@ import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException; import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import com.google.gson.Gson;
import com.google.gson.JsonIOException;
import com.google.gson.JsonParseException;
import com.google.gson.reflect.TypeToken;
/** /**
* Implementation of {@link org.springframework.http.converter.HttpMessageConverter * Implementation of {@link org.springframework.http.converter.HttpMessageConverter}
* HttpMessageConverter} that can read and write JSON using the <a * that can read and write JSON using the
* href="https://code.google.com/p/google-gson/">Google Gson</a> library's {@link Gson} * <a href="https://code.google.com/p/google-gson/">Google Gson</a> library's
* class. * {@link Gson} class.
* *
* <p>This converter can be used to bind to typed beans or untyped * <p>This converter can be used to bind to typed beans or untyped {@code HashMap}s.
* {@link java.util.HashMap HashMap} instances. * By default, it supports {@code application/json} and {@code application/*+json}.
*
* <p>By default this converter supports {@code application/json} and
* {@code application/*+json} but {@link #setSupportedMediaTypes
* supportedMediaTypes} can be used to change that.
* *
* <p>Tested against Gson 2.2; compatible with Gson 2.0 and higher. * <p>Tested against Gson 2.2; compatible with Gson 2.0 and higher.
* *
* @author Roy Clarkson * @author Roy Clarkson
* @since 4.1 * @since 4.1
* @see #setGson
* @see #setSupportedMediaTypes
*/ */
public class GsonHttpMessageConverter extends AbstractHttpMessageConverter<Object> public class GsonHttpMessageConverter extends AbstractHttpMessageConverter<Object>
implements GenericHttpMessageConverter<Object> { implements GenericHttpMessageConverter<Object> {
@ -75,10 +73,10 @@ public class GsonHttpMessageConverter extends AbstractHttpMessageConverter<Objec
new MediaType("application", "*+json", DEFAULT_CHARSET)); new MediaType("application", "*+json", DEFAULT_CHARSET));
} }
/** /**
* Set the {@code Gson} instance to use. * Set the {@code Gson} instance to use.
* If not set, a default {@link Gson#Gson() Gson} instance is used. * If not set, a default {@link Gson#Gson() Gson} instance is used.
*
* <p>Setting a custom-configured {@code Gson} is one way to take further * <p>Setting a custom-configured {@code Gson} is one way to take further
* control of the JSON serialization process. * control of the JSON serialization process.
*/ */
@ -96,7 +94,6 @@ public class GsonHttpMessageConverter extends AbstractHttpMessageConverter<Objec
/** /**
* Specify a custom prefix to use for JSON output. Default is none. * Specify a custom prefix to use for JSON output. Default is none.
*
* @see #setPrefixJson * @see #setPrefixJson
*/ */
public void setJsonPrefix(String jsonPrefix) { public void setJsonPrefix(String jsonPrefix) {
@ -106,13 +103,11 @@ public class GsonHttpMessageConverter extends AbstractHttpMessageConverter<Objec
/** /**
* Indicate whether the JSON output by this view should be prefixed with "{} &&". * Indicate whether the JSON output by this view should be prefixed with "{} &&".
* Default is {@code false}. * Default is {@code false}.
*
* <p>Prefixing the JSON string in this manner is used to help prevent JSON * <p>Prefixing the JSON string in this manner is used to help prevent JSON
* Hijacking. The prefix renders the string syntactically invalid as a script * Hijacking. The prefix renders the string syntactically invalid as a script
* so that it cannot be hijacked. This prefix does not affect the evaluation * so that it cannot be hijacked. This prefix does not affect the evaluation
* of JSON, but if JSON validation is performed on the string, the prefix * of JSON, but if JSON validation is performed on the string, the prefix
* would need to be ignored. * would need to be ignored.
*
* @see #setJsonPrefix * @see #setJsonPrefix
*/ */
public void setPrefixJson(boolean prefixJson) { public void setPrefixJson(boolean prefixJson) {
@ -159,16 +154,15 @@ public class GsonHttpMessageConverter extends AbstractHttpMessageConverter<Objec
/** /**
* Return the Gson {@link TypeToken} for the specified type. * Return the Gson {@link TypeToken} for the specified type.
*
* <p>The default implementation returns {@code TypeToken.get(type)}, but * <p>The default implementation returns {@code TypeToken.get(type)}, but
* this can be overridden in subclasses to allow for custom generic * this can be overridden in subclasses to allow for custom generic
* collection handling. For instance: * collection handling. For instance:
* <pre class="code"> * <pre class="code">
* protected TypeToken<?> getTypeToken(Type type) { * protected TypeToken<?> getTypeToken(Type type) {
* if (type instanceof Class && List.class.isAssignableFrom((Class<?>) type)) { * if (type instanceof Class && List.class.isAssignableFrom((Class<?>) type)) {
* return new TypeToken<ArrayList<MyBean>>() { * return new TypeToken<ArrayList<MyBean>>() {};
* }; * }
* } else { * else {
* return super.getTypeToken(type); * return super.getTypeToken(type);
* } * }
* } * }