package hxk.util;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.AbstractHttpMessageConverter;
import org.springframework.util.StreamUtils;
public class CustomStringHttpMessageConverter extends AbstractHttpMessageConverter<String> {
public static final Charset DEFAULT_CHARSET = Charset.forName("utf-8");
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 CustomStringHttpMessageConverter() {
this(DEFAULT_CHARSET);
}
/**
* A constructor accepting a default charset to use if the requested content
* type does not specify one.
*/
public CustomStringHttpMessageConverter(Charset defaultCharset) {
super(new MediaType("text", "plain", defaultCharset), MediaType.ALL);
this.defaultCharset = defaultCharset;
this.availableCharsets = new ArrayList<Charset>(Charset.availableCharsets().values());
}
/**
* Indicates whether the {@code Accept-Charset} should be written to any outgoing request.
* <p>Default is {@code true}.
*/
public void setWriteAcceptCharset(boolean writeAcceptCharset) {
this.writeAcceptCharset = writeAcceptCharset;
}
@Override
public boolean supports(Class<?> clazz) {
return String.class.equals(clazz);
}
@Override
protected String readInternal(Class<? extends String> clazz, HttpInputMessage inputMessage) throws IOException {
Charset charset = getContentTypeCharset(inputMessage.getHeaders().getContentType());
return StreamUtils.copyToString(inputMessage.getBody(), charset);
}
@Override
protected Long getContentLength(String s, MediaType contentType) {
Charset charset = getContentTypeCharset(contentType);
try {
return (long) s.getBytes(charset.name()).length;
}
catch (UnsupportedEncodingException ex) {
// should not occur
throw new IllegalStateException(ex);
}
}
@Override
protected void writeInternal(String s, HttpOutputMessage outputMessage) throws IOException {
if (this.writeAcceptCharset) {
outputMessage.getHeaders().setAcceptCharset(getAcceptedCharsets());
}
Charset charset = getContentTypeCharset(outputMessage.getHeaders().getContentType());
StreamUtils.copy(s, charset, outputMessage.getBody());
}
/**
* Return the list of supported {@link Charset}.
* <p>By default, returns {@link Charset#availableCharsets()}. Can be overridden in subclasses.
* @return the list of accepted charsets
*/
protected List<Charset> getAcceptedCharsets() {
return this.availableCharsets;
}
private Charset getContentTypeCharset(MediaType contentType) {
if (contentType != null && contentType.getCharSet() != null) {
return contentType.getCharSet();
}
else {
return this.defaultCharset;
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
SpringMVC的Restful风格Demo.zip (72个子文件)
Restful
.project 1KB
WebContent
WEB-INF
lib
servlet-api.jar 173KB
slf4j-log4j12-1.6.6.jar 9KB
commons-io.jar 82KB
slf4j-jdk14-1.6.6.jar 9KB
hibernate-validator-4.3.0.Final.jar 465KB
javassist-3.7.ga.jar 531KB
dom4j-1.6.1.jar 307KB
spring-web-3.2.4.RELEASE.jar 611KB
gson-1.4.jar 162KB
commons-httpclient-3.1.jar 298KB
json-lib-2.1-jdk15.jar 293KB
commons-pool.jar 61KB
spring-tx-3.2.4.RELEASE.jar 237KB
slf4j-jcl-1.6.6.jar 8KB
cglib-nodep-2.1_3.jar 317KB
spring-beans-3.2.4.RELEASE.jar 594KB
jstl.jar 20KB
validation-api-1.1.0.Alpha1.jar 51KB
aopalliance-alpha1.jar 22KB
jackson-mapper-asl-1.7.2.jar 610KB
spring-context-3.2.4.RELEASE.jar 843KB
commons-beanutils-1.7.0.jar 184KB
slf4j-simple-1.6.6.jar 10KB
log4j-1.2.17.jar 478KB
commons-dbcp.jar 119KB
ezmorph-1.0.3.jar 76KB
jsp-api.jar 86KB
commons-codec.jar 46KB
commons-collections-3.1.jar 546KB
slf4j-api-1.6.6.jar 26KB
slf4j-ext-1.6.6.jar 41KB
xmlbeans-2.3.0.jar 2.54MB
spring-core-3.2.4.RELEASE.jar 849KB
spring-aspects-3.2.4.RELEASE.jar 50KB
spring-expression-3.2.4.RELEASE.jar 192KB
jackson-core-asl-1.7.2.jar 203KB
commons-fileupload.jar 52KB
httpcore-4.1.2.jar 177KB
aspectjweaver-1.6.9.jar 1.59MB
commons-logging.jar 44KB
aspectj-1.6.10.jar 12.12MB
ejb3-persistence.jar 49KB
aopalliance-1.0.jar 4KB
jboss-logging-3.1.0.GA.jar 59KB
spring-webmvc-3.2.4.RELEASE.jar 622KB
standard.jar 384KB
slf4j-nop-1.6.6.jar 5KB
spring-aop-3.2.4.RELEASE.jar 328KB
commons-lang-2.5.jar 273KB
httpclient-4.1.2.jar 344KB
slf4j-migrator-1.6.6.jar 28KB
commons-collections-2.1.1.jar 171KB
jta.jar 9KB
springmvc.xml 3KB
web.xml 4KB
META-INF
MANIFEST.MF 39B
src
hxk
action
StudentAction.java 2KB
model
Student.java 618B
util
CustomStringHttpMessageConverter.java 3KB
interceptor
LoginInterceptor.java 610B
.settings
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.common.project.facet.core.xml 305B
org.eclipse.jdt.core.prefs 364B
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.component 473B
.jsdtscope 503B
build
classes
hxk
action
StudentAction.class 3KB
model
Student.class 901B
util
CustomStringHttpMessageConverter.class 5KB
interceptor
LoginInterceptor.class 1KB
.classpath 588B
共 72 条
- 1
distinys
- 粉丝: 2
- 资源: 108
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页