码迷,mamicode.com
首页 > 编程语言 > 详细

002 Spring Restful案例

时间:2015-09-06 21:26:49      阅读:293      评论:0      收藏:0      [点我收藏+]

标签:

1:工程结构

技术分享

需要注意的是需要额外导入以下三个包:

jackson-annotations-2.6.1.jar

jackson-core-2.6.1.jar 

jackson-databind-2.6.1.jar

 

2:配置文件

技术分享

(1)web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    
    <!--Spring Restfull-->
    <servlet>
        <servlet-name>rest</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
     <servlet-name>rest</servlet-name>
     <url-pattern>/*</url-pattern>
    </servlet-mapping>
    
</web-app>

(2)rest-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:context="http://www.springframework.org/schema/context" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans           
        http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 
        http://www.springframework.org/schema/context             
        http://www.springframework.org/schema/context/spring-context-4.2.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
        http://www.springframework.org/schema/jee 
    http://www.springframework.org/schema/jee/spring-jee-4.2.xsd">  
   
    <mvc:annotation-driven/>
    <context:component-scan base-package="com.wzh.api"/> 
    
    <!--Dao-->
    <bean id="userDao" class="com.wzh.api.user.UserDaoImpl"/>
    
    <!--Service-->
    <bean id="userService" class="com.wzh.api.user.UserService">
        <property name="userDao" ref="userDao"/>
    </bean>
    
    <!--Controller-->
    <bean id="userCtrl" class="com.wzh.api.user.UserCtrl">
         <property name="userService" ref="userService"/>
    </bean>
    
</beans>

3:com.wzh.comm.ResultVo

package com.wzh.comm;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;

/**
 * @author  y
 * @date    2015-9-6 18:34:44
 * @version V1.0
 * @desc    操作结果公共返回值对象
 */
public class ResultVo {
    @JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
    private Integer no;
    
    @JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
    private String msg;
    
    @JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
    private Object result;
    
    public Integer getNo() {
        return no;
    }

    public void setNo(Integer no) {
        this.no = no;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getResult() {
        return result;
    }

    public void setResult(Object result) {
        this.result = result;
    }
    
    
}

使用jackson提供的注解功能:

@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)

如果字段的值为NULL则不进行JOSN转换.

 

4:com.wzh.api.user.*

(1)UserModel.java

package com.wzh.api.user;

/**
 * @author  y
 * @date    2015-9-5 9:15:55
 * @version V1.0
 * @desc    用户对象
 */
public class UserModel {

    private String dm;
    private String name;
    private Integer age;

    public String getDm() {
        return dm;
    }

    public void setDm(String dm) {
        this.dm = dm;
    }
    
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
    
    
}

 

(2)UserDao.java

package com.wzh.api.user;

import java.util.List;

/**
 * @author  y
 * @date    2015-9-5 9:17:28
 * @version 1.0
 * @desc    
 */
public interface UserDao {

    int add(UserModel user);
    
    int delete(String dm);
    
    int update(UserModel user);
    
    UserModel get(String dm);
    
    List<UserModel> list(UserModel user,int offset,int count);
}

 

(3)UserDaoImpl.java

package com.wzh.api.user;

import java.util.ArrayList;
import java.util.List;

/**
 * @author  y
 * @date    2015-9-6 18:20:23
 * @version V1.0
 * @desc    
 */
public class UserDaoImpl implements UserDao{

    public int add(UserModel user) {
        return 1;
    }

    public int delete(String dm) {
        return 1;
    }

    public int update(UserModel user) {
        return 1;
    }

    public UserModel get(String dm) {
        UserModel um = new UserModel();
        um.setDm(dm);
        um.setName("张三");
        um.setAge(26);
        return um;
    }

    public List<UserModel> list(UserModel user, int offset, int count) {
        List<UserModel> list = new ArrayList<UserModel>();
        UserModel um = new UserModel();
        um.setDm("001");
        um.setName("张三");
        um.setAge(26);
        list.add(um);
        um = new UserModel();
        um.setDm("002");
        um.setName("李四");
        um.setAge(26);
        list.add(um);
        um = new UserModel();
        um.setDm("003");
        um.setName("王五");
        um.setAge(26);
        list.add(um);
        
        return list;
    }

}

 

(4)UserService.java

package com.wzh.api.user;

import com.wzh.comm.ResultVo;


/**
 * @author  y
 * @date    2015-9-6 18:24:13
 * @version V1.0
 * @desc    
 */
public class UserService {
    private UserDao userDao;
    private ResultVo resultVo;
    
    public void setUserDao(UserDao userDao){
        this.userDao = userDao;
    }
    
    
    public ResultVo add(UserModel user){
        resultVo = new ResultVo();
        resultVo.setNo(userDao.add(user));
        resultVo.setMsg("数据增加成功");
        
        return resultVo;
    }
    
    public ResultVo delete(String dm){
        resultVo = new ResultVo();
        resultVo.setNo(userDao.delete(dm));
        resultVo.setMsg("删除成功");
        
        return resultVo;
    }
    
    public ResultVo update(UserModel user){
        resultVo = new ResultVo();
        resultVo.setNo(userDao.update(user));
        resultVo.setMsg("更新成功");
        
        return resultVo;
    }
    
    public ResultVo get(String dm){
        resultVo = new ResultVo();
        resultVo.setNo(1);
        resultVo.setMsg("OK");
        resultVo.setResult(userDao.get(dm));
        
        return resultVo;
    }
    
    public ResultVo list(UserModel user,int offset,int count){
        resultVo = new ResultVo();
        resultVo.setNo(1);
        resultVo.setMsg("OK");
        resultVo.setResult(userDao.list(user, offset, count));
        
        return resultVo;
    }
}

 

(5)UserCtrl.java

package com.wzh.api.user;

import com.wzh.comm.ResultVo;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author  y
 * @date    2015-9-6 18:49:03
 * @version V1.0
 * @desc    
 */
@RestController
@RequestMapping("/api/users")
public class UserCtrl {

    private UserService userService;
    
    public void setUserService(UserService userService){
        this.userService = userService;
    }
    
    @RequestMapping(
            value="/add/{dm}/{name}/{age}",
            method=RequestMethod.POST,
            headers="Accept=application/json")
    public ResultVo add(@PathVariable String dm,@PathVariable String name,@PathVariable Integer age){
        UserModel user = new UserModel();
        user.setDm(dm);
        user.setName(name);
        user.setAge(age);
        
        return userService.add(user);
    }
    
    @RequestMapping(
            value="/{dm}",
            method=RequestMethod.DELETE,
            headers="Accept=application/json")
    public ResultVo delete(@PathVariable String dm){
        return userService.delete(dm);
    }
    
    @RequestMapping(
            value="/update/{dm}/{name}/{age}",
            method=RequestMethod.PUT,
            headers="Accept=application/json")
    public ResultVo update(@PathVariable String dm,@PathVariable String name,@PathVariable Integer age){
        UserModel user = new UserModel();
        user.setDm(dm);
        user.setName(name);
        user.setAge(age);
        
        return userService.update(user);
    }
    
    @RequestMapping(
            value="/{dm}",
            method=RequestMethod.GET,
            headers="Accept=application/json")
    public ResultVo get(@PathVariable String dm){
        return userService.get(dm);
    }
    
    @RequestMapping(
            method=RequestMethod.GET,
            headers="Accept=application/json")
    public ResultVo list(){
        UserModel user = new UserModel();
        int offset=10;
        int count = 10;
        
        return userService.list(user, offset, count);
    } 
}

 

6:测试

使用google一个插件应用Postman进行Restful测试.

(1)增加用户

技术分享

(2)删除用户

技术分享

(3)更新用户信息

技术分享

(4)查询一个用户的详情

技术分享

(5)查询集合用户详情

技术分享

 

002 Spring Restful案例

标签:

原文地址:http://www.cnblogs.com/yshyee/p/4787090.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!