Skip to content

Commit a4240d2

Browse files
committed
Add defaultCharset field to StringHttpMessageConverter
Before this change the StringHttpMessageConverter used a fixed charset "ISO-8859-1" if the requested content type did not specify one. This change adds a defaultCharset field and a constructor to configure it in StringHttpMessageConverter. Issue: SPR-9487
1 parent 4d297b4 commit a4240d2

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -41,14 +41,27 @@
4141
*/
4242
public class StringHttpMessageConverter extends AbstractHttpMessageConverter<String> {
4343

44-
public static final Charset DEFAULT_CHARSET = Charset.forName("ISO-8859-1");
44+
private final Charset defaultCharset;
4545

4646
private final List<Charset> availableCharsets;
4747

4848
private boolean writeAcceptCharset = true;
4949

50+
/**
51+
* A default constructor that uses {@code "ISO-8859-1"} as the default charset.
52+
* @see #StringHttpMessageConverter(Charset)
53+
*/
5054
public StringHttpMessageConverter() {
51-
super(new MediaType("text", "plain", DEFAULT_CHARSET), MediaType.ALL);
55+
this(Charset.forName("ISO-8859-1"));
56+
}
57+
58+
/**
59+
* A constructor accepting a default charset to use if the requested content
60+
* type does not specify one.
61+
*/
62+
public StringHttpMessageConverter(Charset defaultCharset) {
63+
super(new MediaType("text", "plain", defaultCharset), MediaType.ALL);
64+
this.defaultCharset = defaultCharset;
5265
this.availableCharsets = new ArrayList<Charset>(Charset.availableCharsets().values());
5366
}
5467

@@ -108,7 +121,7 @@ private Charset getContentTypeCharset(MediaType contentType) {
108121
return contentType.getCharSet();
109122
}
110123
else {
111-
return DEFAULT_CHARSET;
124+
return this.defaultCharset;
112125
}
113126
}
114127

src/dist/changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Changes in version 3.2 M2 (2012-08-xx)
1616
* added support for the HTTP PATCH method
1717
* enable smart suffix pattern match in @RequestMapping methods (SPR-7632)
1818
* DispatcherPortlet does not forward event exceptions to the render phase by default (SPR-9287)
19+
* add defaultCharset property to StringHttpMessageConverter
1920

2021

2122
Changes in version 3.2 M1 (2012-05-28)

0 commit comments

Comments
 (0)