Browse Source

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
pull/104/head
Rossen Stoyanchev 14 years ago
parent
commit
a4240d2864
  1. 21
      spring-web/src/main/java/org/springframework/http/converter/StringHttpMessageConverter.java
  2. 1
      src/dist/changelog.txt

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2011 the original author or authors.
* Copyright 2002-2012 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.
@ -41,14 +41,27 @@ import org.springframework.util.FileCopyUtils; @@ -41,14 +41,27 @@ import org.springframework.util.FileCopyUtils;
*/
public class StringHttpMessageConverter extends AbstractHttpMessageConverter<String> {
public static final Charset DEFAULT_CHARSET = Charset.forName("ISO-8859-1");
private final Charset defaultCharset;
private final List<Charset> availableCharsets;
private boolean writeAcceptCharset = true;
/**
* A default constructor that uses {@code "ISO-8859-1"} as the default charset.
* @see #StringHttpMessageConverter(Charset)
*/
public StringHttpMessageConverter() {
super(new MediaType("text", "plain", DEFAULT_CHARSET), MediaType.ALL);
this(Charset.forName("ISO-8859-1"));
}
/**
* A constructor accepting a default charset to use if the requested content
* type does not specify one.
*/
public StringHttpMessageConverter(Charset defaultCharset) {
super(new MediaType("text", "plain", defaultCharset), MediaType.ALL);
this.defaultCharset = defaultCharset;
this.availableCharsets = new ArrayList<Charset>(Charset.availableCharsets().values());
}
@ -108,7 +121,7 @@ public class StringHttpMessageConverter extends AbstractHttpMessageConverter<Str @@ -108,7 +121,7 @@ public class StringHttpMessageConverter extends AbstractHttpMessageConverter<Str
return contentType.getCharSet();
}
else {
return DEFAULT_CHARSET;
return this.defaultCharset;
}
}

1
src/dist/changelog.txt vendored

@ -16,6 +16,7 @@ Changes in version 3.2 M2 (2012-08-xx) @@ -16,6 +16,7 @@ Changes in version 3.2 M2 (2012-08-xx)
* added support for the HTTP PATCH method
* enable smart suffix pattern match in @RequestMapping methods (SPR-7632)
* DispatcherPortlet does not forward event exceptions to the render phase by default (SPR-9287)
* add defaultCharset property to StringHttpMessageConverter
Changes in version 3.2 M1 (2012-05-28)

Loading…
Cancel
Save