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案例的更多相关文章

  1. HTTP Status 405 - Request method 'GET' not supported?(尚硅谷Restful案例练习关于Delete方法出现的错误)

    哈罗大家好,最近在如火如荼的学习java开发----Spring系列框架,当学习到SpringMVC,动手实践RESTFUL案例时,发现了以上报错405,get请求方法没有被支持. 首先第一步,我查看 ...

  2. cxf spring restful 问题解决(jar包冲突)

    SEVERE: Context initialization failedorg.springframework.beans.factory.BeanCreationException: Error ...

  3. spring原理案例-基本项目搭建 01 spring framework 下载 官网下载spring jar包

    下载spring http://spring.io/ 最重要是在特征下面的这段话,需要注意: All avaible features and modules are described in the ...

  4. spring原理案例-基本项目搭建 02 spring jar包详解 spring jar包的用途

    Spring4 Jar包详解 SpringJava Spring AOP: Spring的面向切面编程,提供AOP(面向切面编程)的实现 Spring Aspects: Spring提供的对Aspec ...

  5. spring原理案例-基本项目搭建 03 创建工程运行测试 spring ioc原理实例示例

    下面开始项目的搭建 使用 Java EE - Eclipse 新建一 Dynamic Web Project Target Runtime 选 Apache Tomcat 7.0(不要选 Apache ...

  6. spring入门--spring入门案例

    spring是一个框架,这个框架可以干很多很多的事情.感觉特别吊.但是,对于初学者来说,很难理解spring到底是干什么的.我刚开始的时候也不懂,后来就跟着敲,在后来虽然懂了,但是依然说不明白它到底是 ...

  7. Spring入门案例 idea创建Spring项目

    spring入门案例 idea创建spring项目 Spring介绍 Spring概述 Spring是一个开源框架,Spring是2003年兴起的轻量级java开发框架,由Rod Johnson 在其 ...

  8. Spring(二)--Spring入门案例

    Spring入门案例 1.需要的实体类 2.需要的接口和实现类 3.需要的service和实现类 /** * service层的作用 * 在不改变dao层代码的前提下,增加业务逻辑操作 */ publ ...

  9. Spring升级案例之IOC介绍和依赖注入

    Spring升级案例之IOC介绍和依赖注入 一.IOC的概念和作用 1.什么是IOC 控制反转(Inversion of Control, IoC)是一种设计思想,在Java中就是将设计好的对象交给容 ...

随机推荐

  1. crossfire 346# B

    Vasya has the square chessboard of size n × n and m rooks. Initially the chessboard is empty. Vasya ...

  2. QT的信号与槽机制介绍

      信号与槽作为QT的核心机制在QT编程中有着广泛的应用,本文介绍了信号与槽的一些基本概念.元对象工具以及在实际使用过程中应注意的一些问题. QT是一个跨平台的C++ GUI应用构架,它提供了丰富的窗 ...

  3. Oracle V$SQLAREA

    V$SQLAREA 记录shared SQL area中语句统计信息: V$SQLAREA持续跟踪所有shared pool中的共享cursor,在shared pool中的每一条SQL语句都对应一列 ...

  4. service redis does not support chkconfig的解决办法

    原文链接: http://my.oschina.net/maczhao/blog/322931 问题解决办法如下: 必须把下面两行注释放在/etc/init.d/redis文件靠前的注释中: # ch ...

  5. Android系统移植与驱动开发——第七章——LED驱动

    LED驱动的实现原理 编写LED驱动: 测试LED驱动之前需要用USB数据线连接开发板,然后打开电源,成功启动之后,执行build.sh脚本文件编译和安装LED驱动,顺利则会自动连接 如果有多个设备文 ...

  6. codevs 1200 同余方程 (Extend_Eulid)

    /* 扩展欧几里得 ax%b==1 -> ax-by==1 求不定方程的一组解 使x为最小正整数解 */ #include<iostream> #include<cstdio& ...

  7. Entity Framework 6 学习笔记

    自引用 public class PictureCategory { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] publi ...

  8. RESTful Web Services简单介绍

    近几年,RESTful Web Services渐渐开始流行,大量用于解决异构系统间的通信问题.很多网站和应用提供的API,都是基于RESTful风格的Web Services,比较著名的包括Twit ...

  9. LINUX系统安装MYSQL命令,纯手打

    1.下载安装包 wget http://cdn.mysql.com/archives/mysql-5.6/mysql-5.6.26-linux-glibc2.5-x86_64.tar.gz    2. ...

  10. TalkingData游戏版本在Cocos2d-x 3.2使用

    最近一直忙别的方面的事情,没有太关注cocos2dx的发展情况,竟然已经更新到了3.2的版本,总的来说3.2比较3.0在使用上会有一些路径的变成,包括ios的引用路径和android上的build的p ...