相關的jar包支持,jackson。php
如果Maven工程,便可直接在pom.xml中添加一下依賴便可:web
- <dependency>
- <groupId>com.fasterxml.jackson.core</groupId>
- <artifactId>jackson-databind</artifactId>
- <version>2.5.0</version>
- </dependency>
- <dependency>
- <groupId>com.fasterxml.jackson.core</groupId>
- <artifactId>jackson-core</artifactId>
- <version>2.5.0</version>
- </dependency>
- <dependency>
- <groupId>com.fasterxml.jackson.core</groupId>
- <artifactId>jackson-annotations</artifactId>
- <version>2.5.0</version>
- </dependency>
如果傳統工程,直接下載這三個jar包並添加到工程中便可。spring
官網的內容視圖解析器實例配置:json
這裏,咱們就直接拷貝下來放進applicationContext.xml中:app
- <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
- <property name="viewResolvers">
- <list>
- <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />
- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="prefix" value="/WEB-INF/jsp/" />
- <property name="suffix" value=".jsp" />
- </bean>
- </list>
- </property>
- <property name="defaultViews">
- <list>
- <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" />
- </list>
- </property>
- </bean>
如今咱們將接口方法返回值改成map便可返回json格式的數據了:jsp
搭建完整的spring4MVC工程,請參考:Eclipse搭建Maven Web工程集成Spring4 MVC 附源碼下載spa