项目的目录结构如下:
1.流程图
略。。
2.服务端代码
①实体类
package com.bjsxt.model; import java.io.Serializable; public class Person implements Serializable{ /** * */ private static final long serialVersionUID = -3727979363425652597L; private int id; private String name; private String sex; private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
②接口编码IPersonService
package com.bjsxt.server; import javax.ws.rs.core.MediaType; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import com.bjsxt.model.Person; /** * spring MVC风格的restful接口 * * @author gaoweignag * @since JDK1.7 */ @RequestMapping("/test") @Controller public interface IPersonService { @RequestMapping(value = "/hello", produces = "text/plain;charset=UTF-8") public @ResponseBody String hello(); @RequestMapping(value = "/say/{msg}", produces = "application/json;charset=UTF-8") public @ResponseBody String say(@PathVariable(value = "msg") String msg); @RequestMapping(value = "/person/{id:\\d+}", method = RequestMethod.GET, produces = "application/json;charset=UTF-8") public @ResponseBody String getPerson(@PathVariable("id") int id); @RequestMapping(value = "/person/{id:\\d+}", method = RequestMethod.DELETE) public @ResponseBody Object deletePerson(@PathVariable("id") int id) ; /** * 推荐使用,这种可以解决绝大多数问题 * @param person * @return */ @RequestMapping(value = "/person", method = RequestMethod.POST, produces = {MediaType.APPLICATION_JSON,"application/json;charset=UTF-8"}, consumes = {MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) public String addPerson(Person person); @RequestMapping(value = "/person", method = RequestMethod.PUT) public @ResponseBody Object updatePerson(@RequestBody Person person); }
③接口实现类PersonService
package com.bjsxt.server.impl; import java.util.ArrayList; import java.util.List; import javax.ws.rs.core.MediaType; 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.RequestBody; 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.bjsxt.model.Person; import com.bjsxt.server.IPersonService; /** * spring MVC风格的restful接口 * * @author gaoweignag * @since JDK1.7 */ @Controller public class PersonService implements IPersonService{ /** 日志实例 */ private static final Logger logger = Logger.getLogger(PersonService.class); public @ResponseBody String hello() { logger.info("hello........"); return "你好!hello"; } public @ResponseBody String say(@PathVariable(value = "msg") String msg) { return "{\"msg\":\"you say:'" + msg + "'\"}"; } public @ResponseBody String getPerson(@PathVariable("id") int id) { logger.info("获取人员信息id=" + id); Person person = new Person(); person.setName("张三"); person.setSex("男"); person.setAge(30); person.setId(id); JSONObject jsonObject = JSONObject.fromObject(person); logger.info(jsonObject); logger.info(jsonObject.toString()); return jsonObject.toString(); } public Object deletePerson(@PathVariable("id") int id) { logger.info("删除人员信息id=" + id); JSONObject jsonObject = new JSONObject(); jsonObject.put("msg", "删除人员信息成功"); return jsonObject; } public @ResponseBody String addPerson(@RequestBody Person person) { logger.info("注册人员信息成功id=" + person.getId()); JSONObject jsonObject = new JSONObject(); jsonObject.put("msg", "注册人员信息成功"); return jsonObject.toString(); } public @ResponseBody Object updatePerson(@RequestBody Person person) { logger.info("更新人员信息id=" + person.getId()); JSONObject jsonObject = new JSONObject(); jsonObject.put("msg", "更新人员信息成功"); return jsonObject.toString(); } }
④配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>springMVCRestful</display-name> <servlet> <servlet-name>springMVCRestful</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMVCRestful</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 如果有乱码我们则需要配置字符编码集的过滤器来防止乱码问题 --> <filter> <filter-name>encodingFilter</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>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
⑤配置spring mvc文件springMVCRestful-servlete.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd" default-autowire="byName" > <!-- 初始化com.bjsxt目录下面的bean --> <context:component-scan base-package="com.bjsxt"></context:component-scan> <!-- 消息适配器 --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <!-- json转换器 --> <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" /> </list> </property> </bean> <!--配置spring MVC的注解 驱动 --> <mvc:annotation-driven/> </beans>
3.客户端代码PersonClientTest,使用junit进行单元测试
package com.bjsxt.client; import org.apache.log4j.Logger; import org.junit.BeforeClass; import org.junit.Test; import org.springframework.web.client.RestClientException; import org.springframework.web.client.RestTemplate; import com.bjsxt.model.Person; public class PersonClientTest { private static Logger LOG = Logger.getLogger(PersonClientTest.class); private static RestTemplate template = null; @BeforeClass public static void beforeClass(){ template = new RestTemplate(); } @Test public void testHello(){ LOG.info("进入hello Method start..."); String url = "http://localhost:8080/springMVCRestful/test/hello"; /** * 第一个参数是restful接口请求路径 * 第二个参数是响应的类型 String.class */ String result = template.getForObject(url, String.class); LOG.info("输出结果:"+result); LOG.info("进入hello Method end..."); } @Test public void testSay(){ LOG.info("进入say Method start..."); String url = "http://localhost:8080/springMVCRestful/test/say/gaoweigang"; String result = template.getForObject(url, String.class); LOG.info("输出结果:"+result); LOG.info("进入say Method end..."); } @Test public void testGetPerson(){ LOG.info("进入getPerson Method start..."); /** * restful参数类型是int,不能传String类型的参数,否则调用不到接口 */ String url = "http://localhost:8080/springMVCRestful/test/person/101"; String result = template.getForObject(url, String.class); LOG.info("输出结果:"+result); LOG.info("进入getPerson Method end..."); } @Test public void testDeletePerson(){ LOG.info("进入deletePerson Method start..."); /** * restful参数类型是int,不能传String类型的参数,否则调用不到接口 */ String url = "http://localhost:8080/springMVCRestful/test/person/1234"; try { template.delete(url); } catch (RestClientException e) { e.printStackTrace(); } LOG.info("进入deletePerson Method end..."); } @Test public void testUpdatePerson(){ LOG.info("进入UpdatePerson Method start..."); /** * restful参数类型是int,不能传String类型的参数,否则调用不到接口 */ String url = "http://localhost:8080/springMVCRestful/test/person"; try { Person person =new Person(); person.setId(1234); person.setName("gaoweigang"); person.setAge(22); person.setSex("男"); template.put(url, person); } catch (RestClientException e) { e.printStackTrace(); } LOG.info("进入UpdatePerson Method end..."); } @Test public void testAddPerson(){ LOG.info("进入addPerson Method start..."); /** * restful参数类型是int,不能传String类型的参数,否则调用不到接口 */ String url = "http://localhost:8080/springMVCRestful/test/person"; Person person =new Person(); person.setId(1234); person.setName("gaoweigang"); person.setAge(22); person.setSex("男"); String result = template.postForObject(url, person, String.class); LOG.info("输出结果为:"+result); LOG.info("进入addPerson Method end..."); } }
4.启动tomcat服务后,就可以对PersonClientTest进行单元测试了
可能遇到的问题:
1.The prefix "mvc" for element "mvc:annotation-driven" is not bound 的解决方法
在spring mvc在配置文件中添加namespace
xmlns:mvc="http://www.springframework.org/schema/mvc" http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
相关推荐
基于 Spring Boot,Spring MVC,Spring Data 的企业Web应用快速开发框架。 为什么要有 SwiftBoot 虽然用 SpringBoot 搭建框架进行开发已经简化了很多配置的工作,但是对于大多数中小型的项目来说来,还是有一些重复...
使用 Spring 可插入的 MVC 架构,从而在使用 Spring 进行 WEB 开发时,可以选择使用 Spring的 Spring MVC 框架或集成其他 MVC 开发框架,如 Struts1(现在一般不用),Struts2 等。SpringMVC 已经成为目前最主流的 MVC...
代码设计风格基于RESTful,以c3p0作为连接池,Redis数据库为媒介实现高并发技术。其中,对于相关的DAO,Service操作,均添加了Junit单元测试实例。 开发文档 一、业务分析 1.秒杀系统业务流程 2.秒杀业务的核心...
我的专栏中有关于RESTful API 开发的文章,自己码的代码,创作不易。 详细、具体、可运行,帮助你快速上手RESTful API 。 构建一个标准的Spring boot项目,使用web环境。 我们用的是Spring boot 版本的MVC ,spring ...
基于 SSM + VUE 开发的目前最火爆的B2C 电商...接口文档引擎:Swagger2 RESTful 风格 API 文档生成 全文检索引擎:Elasticsearch 系统任务调度:Quartz 前后分离 前端框架:NodeJS + Vue + Axios 前端模板:ElementUI
* 接口文档引擎:Swagger2 RESTful 风格 API 文档生成 * 全文检索引擎:ElasticSearch (后期考虑加入) * 分布式链路追踪:ZipKin * 分布式文件系统:Alibaba FastDFS * 分布式服务监控:Spring Boot Admin * ...
您可以使用Spring MVC来开发这些接口,并使用Spring Data JPA来处理数据库访问。 3. 用户管理:实现用户注册、登录、权限管理等功能。使用Spring Security来处理用户认证和授权。 4. 音乐资源管理:上传/编辑/删除...
本项目,基于SSM(Spring, Spring MVC, MyBatis)和Vue.js开发的商店POS(销售点)积分管理系统,旨在为零售商提供一个全面的解决方案,以便更有效地管理顾客积分,提升销售效率和顾客满意度。 二、项目模块说明 ...
该资源是一个基于微信小程序的网上商城项目,采用SSM框架(Spring、Spring MVC、MyBatis)和Vue.js技术开发。以下是该项目的主要技术介绍和功能: 技术栈: 后端:Java、Spring、Spring MVC、MyBatis 前端:微信小...
RESTful API:设计符合RESTful风格的API接口,实现了前后端分离,提高了系统的可扩展性和灵活性。 JWT 鉴权机制:采用JWT(JSON Web Token)实现用户身份认证和权限控制,保障系统的安全性和稳定性。 通过SaaS OA...
基于java的实现MVC设计模式的请求驱动类型的轻量级Web框架,通过注解,无需实现任何接口,处理请求,支持restful。 三层结构:表现层、业务层、持久层 设计模式:Model(模型)、View(视图)、Controller(控制器)
整个项目采用了RESTful风格进行接口设计,保证了系统的高效性和稳定性。该网站支持管理员对电影信息、影院信息、排片信息进行管理,具有良好的用户体验和管理功能。在技术上采用了Spring Security进行权限控制与安全...
后端为前端提供restful化接口,接入微信SDK接口,用户通过微信扫码登录下单等,用redis存储token对用户进行识别与持久化,使用AOP技术验证卖家端登录身份信息,websocket实现消息推送实时监控订单,使用redis缓存...
开发环境 操作系统:Windows 10 Enterprise 开发工具:Intellij ...接口文档引擎:Swagger2 RESTful 风格 API 文档生成 全文检索引擎:Elasticsearch 系统任务调度:Quartz 前后分离 前端框架:NodeJS + Vue + Axios
SpringBoot、jwt和JwtPermission实现的前后端分离开发框架,接口遵循RESTful风格,相比SpringSecurity和oAuth2.0框架更加轻量级。 在线演示:, 账号:admin,密码:admin。 使用技术 描述 框架 核心框架 Spring、...
SpringBoot、jwt和JwtPermission实现的前后端分离开发框架,接口遵循RESTful风格,相比SpringSecurity和oAuth2.0框架更加轻量级。 在线演示:, 账号:admin,密码:admin。 使用技术 描述 框架 核心框架 Spring、...
SpringBoot、jwt和JwtPermission实现的前后端分离开发框架,接口遵循RESTful风格,相比SpringSecurity和oAuth2.0框架更加轻量级。 在线演示:, 账号:admin,密码:admin。 使用技术 描述 框架 核心框架 Spring、...
在技术选型上,我们采用了Spring Boot作为后端开发框架,利用其快速搭建、简化配置的特性,大幅提高了开发效率。同时,结合MySQL数据库进行数据存储,确保了数据的安全性和稳定性。前端则采用了HTML、CSS和...
采用RESTful API设计风格,确保接口的规范性和扩展性。代码遵循MVC模式,层次清晰,便于维护和扩展。适用场景:该线上辅导班系统适用于教育机构、个人教师或学校,希望快速搭建一个功能全面、操作简便的在线教育平台...
简介BitsCloudDisk(哔呲云盘),一个基于Java开发的公有云盘系统〜初步采用Spring + Spring MVC + Mybatis框架编写,接口使用RESTful风格设计,实现了前拆分;前端采用Bootstrap + jQuery以及一些其他组件编写。...