1
1
/*
2
- * Copyright 2002-2012 the original author or authors.
2
+ * Copyright 2002-2016 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
29
29
* An {@code HttpMessageConverter} that uses {@link StringHttpMessageConverter}
30
30
* for reading and writing content and a {@link ConversionService} for converting
31
31
* the String content to and from the target object type.
32
- * <p>
33
- * By default, this converter supports the media type {@code text/plain} only.
34
- * This can be overridden by setting the
35
- * {@link #setSupportedMediaTypes supportedMediaTypes} property.
36
- * Example of usage:
32
+ *
33
+ * <p>By default, this converter supports the media type {@code text/plain} only.
34
+ * This can be overridden through the {@link #setSupportedMediaTypes supportedMediaTypes}
35
+ * property.
36
+ *
37
+ * <p>A usage example:
37
38
*
38
39
* <pre class="code">
39
40
* <bean class="org.springframework.http.converter.ObjectToStringHttpMessageConverter">
49
50
*/
50
51
public class ObjectToStringHttpMessageConverter extends AbstractHttpMessageConverter <Object > {
51
52
52
- private ConversionService conversionService ;
53
+ private final ConversionService conversionService ;
53
54
54
- private StringHttpMessageConverter stringHttpMessageConverter ;
55
+ private final StringHttpMessageConverter stringHttpMessageConverter ;
55
56
56
57
57
58
/**
58
59
* A constructor accepting a {@code ConversionService} to use to convert the
59
- * (String) message body to/from the target class type. This constructor
60
- * uses {@link StringHttpMessageConverter#DEFAULT_CHARSET} as the default
61
- * charset.
62
- *
60
+ * (String) message body to/from the target class type. This constructor uses
61
+ * {@link StringHttpMessageConverter#DEFAULT_CHARSET} as the default charset.
63
62
* @param conversionService the conversion service
64
63
*/
65
64
public ObjectToStringHttpMessageConverter (ConversionService conversionService ) {
66
65
this (conversionService , StringHttpMessageConverter .DEFAULT_CHARSET );
67
66
}
68
67
69
68
/**
70
- * A constructor accepting a {@code ConversionService} as well as a default
71
- * charset.
72
- *
69
+ * A constructor accepting a {@code ConversionService} as well as a default charset.
73
70
* @param conversionService the conversion service
74
71
* @param defaultCharset the default charset
75
72
*/
76
73
public ObjectToStringHttpMessageConverter (ConversionService conversionService , Charset defaultCharset ) {
77
74
super (new MediaType ("text" , "plain" , defaultCharset ));
78
75
79
- Assert .notNull (conversionService , "conversionService is required" );
76
+ Assert .notNull (conversionService , "ConversionService is required" );
80
77
this .conversionService = conversionService ;
81
78
this .stringHttpMessageConverter = new StringHttpMessageConverter (defaultCharset );
82
79
}
83
80
81
+
84
82
/**
85
83
* Indicates whether the {@code Accept-Charset} should be written to any outgoing request.
86
84
* <p>Default is {@code true}.
@@ -89,6 +87,7 @@ public void setWriteAcceptCharset(boolean writeAcceptCharset) {
89
87
this .stringHttpMessageConverter .setWriteAcceptCharset (writeAcceptCharset );
90
88
}
91
89
90
+
92
91
@ Override
93
92
public boolean canRead (Class <?> clazz , MediaType mediaType ) {
94
93
return this .conversionService .canConvert (String .class , clazz ) && canRead (mediaType );
@@ -106,15 +105,15 @@ protected boolean supports(Class<?> clazz) {
106
105
}
107
106
108
107
@ Override
109
- protected Object readInternal (Class <? extends Object > clazz , HttpInputMessage inputMessage ) throws IOException {
108
+ protected Object readInternal (Class <?> clazz , HttpInputMessage inputMessage ) throws IOException {
110
109
String value = this .stringHttpMessageConverter .readInternal (String .class , inputMessage );
111
110
return this .conversionService .convert (value , clazz );
112
111
}
113
112
114
113
@ Override
115
114
protected void writeInternal (Object obj , HttpOutputMessage outputMessage ) throws IOException {
116
- String s = this .conversionService .convert (obj , String .class );
117
- this .stringHttpMessageConverter .writeInternal (s , outputMessage );
115
+ String value = this .conversionService .convert (obj , String .class );
116
+ this .stringHttpMessageConverter .writeInternal (value , outputMessage );
118
117
}
119
118
120
119
@ Override
0 commit comments