admin管理员组文章数量:1794759
Spring MVC 实现RESTful 返回JSON格式数据
在Java后端的日常开发中,系统间调用通过使用JSON格式数据,本文将向你展示如何将对象转换成json格式并通过Spring MVC框架返回给调用者。
开发工具配置- Spring 4.2.7.RELEASE
- Jackson 2.6.7
- JDK 1.7
- IntelliJ IDEA 15
- Maven 3.3.9
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="www.springframework/schema/beans" xmlns:xsi="www.w3/2001/XMLSchema-instance" xmlns:jee="www.springframework/schema/jee" xmlns:tx="www.springframework/schema/tx" xmlns:context="www.springframework/schema/context" xmlns:mvc="www.springframework/schema/mvc" xmlns:task="www.springframework/schema/task" xmlns:util="www.springframework/schema/util" xmlns:jaxws="cxf.apache/jaxws" xsi:schemaLocation=" www.springframework/schema/beans www.springframework/schema/beans/spring-beans.xsd www.springframework/schema/util www.springframework/schema/util/spring-util.xsd www.springframework/schema/context www.springframework/schema/context/spring-context.xsd" default-lazy-init="false"> <util:properties id="db" location="classpath:mysql.properties"/> <context:component-scan base-package="com.ricky.codelab" /> <context:annotation-config /> <import resource="spring-mvc.xml"/> </beans>spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="www.springframework/schema/beans" xmlns:xsi="www.w3/2001/XMLSchema-instance" xmlns:context="www.springframework/schema/context" xmlns:mvc="www.springframework/schema/mvc" xsi:schemaLocation="www.springframework/schema/beans www.springframework/schema/beans/spring-beans.xsd www.springframework/schema/context www.springframework/schema/context/spring-context-4.1.xsd www.springframework/schema/mvc www.springframework/schema/mvc/spring-mvc-4.1.xsd"> <context:component-scan base-package="com.ricky.codelab.springmvc.controller"></context:component-scan> <mvc:annotation-driven/> <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/pages/" /> <property name="suffix" value=".jsp" /> </bean> </beans> 3、web.xml <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "java.sun/dtd/web-app_2_3.dtd" > <web-app> <display-name>Spring MVC 4.2 Demo</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath*:/applicationContext*.xml; </param-value> </context-param> <filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <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*:spring-mvc.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> 4、Model package com.ricky.codelab.springmvc.domain; /** * ${DESCRIPTION} * * @author Ricky Fung * @create 2016-07-12 16:54 */ public class User { private String username; private String password; private String email; private int age; 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; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } 5、Controller在需要返回json格式数据的Controller method上添加@ResponseBody注解,如下:
package com.ricky.codelab.springmvc.controller; import com.ricky.codelab.springmvc.domain.User; import com.ricky.codelab.springmvc.service.IUserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; /** * Spring MVC RESTful JSON * * @author Ricky Fung * @create 2016-07-13 11:41 */ @Controller @RequestMapping("/user") public class RESTfulJSONController { @Autowired private IUserService userService; /**Spring MVC RESTful JSON**/ @RequestMapping(value = "/view/{username}", method = RequestMethod.GET) @ResponseBody public User view(@PathVariable String username){ System.out.println("view username:"+username); return userService.findUserByName(username); } @RequestMapping(value = "/query", method = RequestMethod.GET) @ResponseBody public User query(@RequestParam(value="username", required=true) String username){ System.out.println("view username:"+username); return userService.findUserByName(username); } }Spring MVC框架会自动帮我们将Object 转换为JSON string。
6、验证结果打开浏览器,访问 localhost:8080/user/view/ricky 或者 localhost:8080/user/query?username=ricky
返回结果:
{ username: “ricky “, password: “123”, email: “ricky @163”, age: 27 }
版权声明:本文标题:Spring MVC 实现RESTful 返回JSON格式数据 内容由林淑君副主任自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.xiehuijuan.com/baike/1686612422a86080.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论