标签:
我想在程序里这么使用
@RequestMapping("/list") @ResponseBody public List<String> getList(){ List<String> list=new ArrayList<String>(); list.add("哈哈"); list.add("哈哈"); list.add("哈哈"); list.add("哈哈"); list.add("哈哈"); return list; }
所以我需要进行一些配置,首先加入依赖
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.6.3</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.6.3</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-core-lgpl</artifactId> <version>1.9.13</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-core-asl</artifactId> <version>1.9.13</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency>
然后配置springmvc的配置文件
<!-- @ResponseBody注解支持-开始 --> <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>application/json;charset=UTF-8</value> </list> </property> </bean> <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" /> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <bean id="utf8StringHttpMessageConverter" class="test.util.UTF8StringHttpMessageConverter" /> <ref bean="mappingJacksonHttpMessageConverter" /> </list> </property> </bean> <!-- @ResponseBody注解支持-结束 -->
注意其中的UTF8StringHttpMessageConverter
public class UTF8StringHttpMessageConverter extends StringHttpMessageConverter { private static final MediaType utf8 = new MediaType("text", "plain", Charset.forName("UTF-8")); private boolean writeAcceptCharset = true; @Override protected MediaType getDefaultContentType(String dumy) { return utf8; } protected List<Charset> getAcceptedCharsets() { return Arrays.asList(utf8.getCharSet()); } protected void writeInternal(String s, HttpOutputMessage outputMessage) throws IOException { if (this.writeAcceptCharset) { outputMessage.getHeaders().setAcceptCharset(getAcceptedCharsets()); } Charset charset = utf8.getCharSet(); FileCopyUtils.copy(s, new OutputStreamWriter(outputMessage.getBody(), charset)); } public boolean isWriteAcceptCharset() { return writeAcceptCharset; } public void setWriteAcceptCharset(boolean writeAcceptCharset) { this.writeAcceptCharset = writeAcceptCharset; } }
到此配置完成,留作备忘。
标签:
原文地址:http://my.oschina.net/shyloveliyi/blog/525309