博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringMVC+Json构建基于Restful风格的应用
阅读量:7256 次
发布时间:2019-06-29

本文共 7336 字,大约阅读时间需要 24 分钟。

摘要
一个Spring使用Restful风格的案例

主要代码:

web.xml

log4jConfigLocation
classpath:log4j.properties
log4jRefreshInterval
60000
contextConfigLocation
classpath:applicationContext.xml
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceEncoding
true
encodingFilter
/*
org.springframework.web.context.ContextLoaderListener
springMVC3
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springMVC-servlet.xml
1
springMVC3
/
HiddenHttpMethodFilter
org.springframework.web.filter.HiddenHttpMethodFilter
HiddenHttpMethodFilter
springMVC3
UikitTest
/WEB-INF/jsp/index.jsp

springMVC-servlet.xml

findJsp
findJsp
text/plain;charset=UTF-8
Controller:

package com.citic.test.action;  import java.util.ArrayList;  import java.util.List;  import net.sf.json.JSONObject;  import org.apache.log4j.Logger;  import org.springframework.stereotype.Controller;  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.RequestParam;  import org.springframework.web.bind.annotation.ResponseBody;  import com.citic.test.entity.Person;  /**  * 基于Restful风格架构测试  */  @Controller  public class DekotaAction {      /** 日志实例 */      private static final Logger logger = Logger.getLogger(DekotaAction.class);      @RequestMapping(value = "/hello", produces = "text/plain;charset=UTF-8")      public @ResponseBody      String hello() {          return "你好!hello";      }      @RequestMapping(value = "/say/{msg}", produces = "application/json;charset=UTF-8")      public @ResponseBody      String say(@PathVariable(value = "msg") String msg) {          return "{\"msg\":\"you say:'" + msg + "'\"}";      }      @RequestMapping(value = "/person/{id:\\d+}", method = RequestMethod.GET)      public @ResponseBody      Person getPerson(@PathVariable("id") int id) {          logger.info("获取人员信息id=" + id);          Person person = new Person();          person.setName("张三");          person.setSex("男");          person.setAge(30);          person.setId(id);          return person;      }      @RequestMapping(value = "/person/{id:\\d+}", method = RequestMethod.DELETE)      public @ResponseBody      Object deletePerson(@PathVariable("id") int id) {          logger.info("删除人员信息id=" + id);          JSONObject jsonObject = new JSONObject();          jsonObject.put("msg", "删除人员信息成功");          return jsonObject;      }      @RequestMapping(value = "/person", method = RequestMethod.POST)      public @ResponseBody      Object addPerson(Person person) {          logger.info("注册人员信息成功id=" + person.getId());          JSONObject jsonObject = new JSONObject();          jsonObject.put("msg", "注册人员信息成功");          return jsonObject;      }      @RequestMapping(value = "/person", method = RequestMethod.PUT)      public @ResponseBody      Object updatePerson(Person person) {          logger.info("更新人员信息id=" + person.getId());          JSONObject jsonObject = new JSONObject();          jsonObject.put("msg", "更新人员信息成功");          return jsonObject;      }      @RequestMapping(value = "/person", method = RequestMethod.PATCH)      public @ResponseBody      List
listPerson(@RequestParam(value = "name", required = false, defaultValue = "") String name) { logger.info("查询人员name like " + name); List
lstPersons = new ArrayList
(); Person person = new Person(); person.setName("张三"); person.setSex("男"); person.setAge(25); person.setId(101); lstPersons.add(person); Person person2 = new Person(); person2.setName("李四"); person2.setSex("女"); person2.setAge(23); person2.setId(102); lstPersons.add(person2); Person person3 = new Person(); person3.setName("王五"); person3.setSex("男"); person3.setAge(27); person3.setId(103); lstPersons.add(person3); return lstPersons; } }

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  <%      String path = request.getContextPath();      String basePath = request.getScheme() + "://"              + request.getServerName() + ":" + request.getServerPort()              + path + "/";  %>          Uikit Test      

Uikit Test

Uikit表单渲染测试
基于Restful架构风格的资源请求测试

转载地址:http://snvdm.baihongyu.com/

你可能感兴趣的文章
阿里、网易、滴滴共十次前端面试碰到的问题
查看>>
【BZOJ】1798: [Ahoi2009]Seq 维护序列seq 线段树多标记(区间加+区间乘)
查看>>
大型项目前端架构详谈(1)纯前端发布
查看>>
【BZOJ】1704: [Usaco2007 Mar]Face The Right Way 自动转身机
查看>>
我的公众号
查看>>
Mysql 相关操作
查看>>
Android 2.0 开机动画文件分析
查看>>
django1.11
查看>>
tornado 07 数据库—ORM—SQLAlchemy—查询
查看>>
UML for mac
查看>>
Python_collections_deque双向队列
查看>>
Graphics View控件在pycharm中显示图片
查看>>
ci实现RBAC,详细解释原理和核心代码显示
查看>>
Qt的SQL操作,DELETE,SELECT
查看>>
分布式系统监视zabbix讲解四之可视化--技术流ken
查看>>
linux上如何删除文件名乱码的文件
查看>>
day07-深浅拷贝 set集合
查看>>
turtle教程-Python绘图
查看>>
BZOJ3530[Sdoi2014]数数——AC自动机+数位DP
查看>>
Ubuntu 安装 TexLive 2011,2010,2009 详细步骤
查看>>