SpringMVC 4.3.4 返回JSON資料
阿新 • • 發佈:2019-02-17
使用Maven管理的依賴庫,
儲存,更新Maven,自動載入依賴庫
建立MvcController,內容如下
web.xml內容如下
applicationContext.xml內容如下
springmvc-servlet.xml內容如下
配置完成之後,啟動專案,訪問http://localhost:8080/webdemo/mvc/json,正確返回json資料。
再訪問原來返回json資料的網址,返回的就是XML格式的資料了
總結:其實Spring4.0之後的版本返回json資料的配置很簡單,只要加入jackson的依賴庫,在controller裡面的對映器方法前使用@ResponseBody註解即可。
如果想返回xml資料,不用新增其他的jar包,只需要在實體類加入 @XmlRootElement 註解類,在controller裡面的對映器方法前使用@ResponseBody註解即可。
在pom.xml的 dependencies 裡面配置Spring和jackson的依賴,配置後,會自動載入,spring context core aop 等其他spring依賴包新增 jackson-databind 後,jackson-core, jackson-annotations也會自動依賴
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.4.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.8.5</version> </dependency>
儲存,更新Maven,自動載入依賴庫
建立MvcController,內容如下
@Controller @RequestMapping("/mvc") public class MvcController { @RequestMapping(value="/json", method=RequestMethod.GET) public @ResponseBody User getUserJson() { User user = new User(); user.setUsername("使用者名稱"); user.setPassword("密碼"); return user; } }
web.xml內容如下
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>Tx-advice</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
applicationContext.xml內容如下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.ing.po"></context:component-scan>
</beans>
springmvc-servlet.xml內容如下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.ing.controller"></context:component-scan>
<mvc:default-servlet-handler />
<bean id="internalResourceViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<mvc:annotation-driven></mvc:annotation-driven>
</beans>
配置完成之後,啟動專案,訪問http://localhost:8080/webdemo/mvc/json,正確返回json資料。
如果想返回XML資料,在實體類上面使用 @XmlRootElement 註解即可,貼出實體類User的原始碼
package com.ing.po;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class User {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
再訪問原來返回json資料的網址,返回的就是XML格式的資料了
總結:其實Spring4.0之後的版本返回json資料的配置很簡單,只要加入jackson的依賴庫,在controller裡面的對映器方法前使用@ResponseBody註解即可。
如果想返回xml資料,不用新增其他的jar包,只需要在實體類加入 @XmlRootElement 註解類,在controller裡面的對映器方法前使用@ResponseBody註解即可。