Spring实现RestFul快速搭建的实例,适合中高级向,不懂可以私信

pom.xml
<!-- Spring hateoas -->
<dependency><groupId>org.springframework.hateoas</groupId><artifactId>spring-hateoas</artifactId><version>0.20.0.RELEASE</version>
</dependency>web.xml
<!-- 上下文配置文件及监听 -->
<context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext-test.xml</param-value>
</context-param>
<listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener><listener-class>org.springframework.web.context.ContextCleanupListener</listener-class>
</listener><servlet><servlet-name>testserver</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping><servlet-name>testserver</servlet-name><url-pattern>/test/*</url-pattern>
</servlet-mapping>applicationContext-test.xml
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:task="http://www.springframework.org/schema/task"xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsdhttp://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"><context:component-scan base-package="com.cunchen"></context:component-scan><bean id="dataSourceForTest" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close"><property name="driverClassName" value="com.mysql.jdbc.Driver" /><property name="url" value="${jdbc.io.url}" /><property name="username" value="${jdbc.io.username}" /><property name="password" value="${jdbc.io.password}" /><property name="defaultAutoCommit" value="false" /><property name="maxTotal" value="25"></property><property name="initialSize" value="15"></property><property name="maxWaitMillis" value="60000"></property><property name="maxIdle" value="20"></property><property name="minIdle" value="15"></property></bean><bean id="sqlSessionFactoryForTest" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSourceForTest" /><property name="typeAliasesPackage" value="com.cunchen.test.entity" /><property name="configLocation" value="classpath:mybatis-config.xml" /><property name="mapperLocations"value="classpath*:com/cunchen/test/mapper/*Mapper.xml" /></bean><bean id="sqlSessionForTest" class="org.mybatis.spring.SqlSessionTemplate"scope="prototype"><constructor-arg index="0" ref="sqlSessionFactoryForTest" /></bean><bean id="transactionManagerForTest"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSourceForTest" /></bean><tx:annotation-driven transaction-manager="transactionManagerForTest" /></beans>testserver-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:p="http://www.springframework.org/schema/p"  xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"  xmlns:util="http://www.springframework.org/schema/util" 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/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">  <!-- 激活注解模式,如@Controller -->  <mvc:annotation-driven />  <!-- 对包中的类的注解进行扫描,创建Bean及自动依赖注入  -->  <context:component-scan base-package="com.turing.phone.control" />  <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />  <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">  <property name="prefix">  <value>/WEB-INF/views/</value>  </property>  <property name="suffix">  <value>.jsp</value>  </property>  </bean>
</beans>  @RestController
@ExposesResourceFor( TestUserInfo.class)
public class BindController {@Resource(name = "testUserInfoDaoImpl")private UserInfoDao userInfoDao;/*** 解除绑定* @param json* @return*/@RequestMapping(value = "/bind", method = RequestMethod.DELETE)public ResponseEntity<String> releaseBind(@RequestBody String json) {PhoneUserInfo userInfo = JSON.toJavaObject(JSONObject.parseObject(json), PhoneUserInfo.class);userInfo.setAuthorization(-1);int i = userInfoDao.updateByPhone(userInfo);if (i < 1) {return new ResponseEntity<String>("There is no info can be updated!", HttpStatus.ACCEPTED);} else {userInfo = userInfoDao.getOne(userInfo);PhoneOutput phoneOutput = new PhoneOutput();phoneOutput.setCount(1);phoneOutput.setUserInfo(userInfo);return new ResponseEntity<String>(JSON.toJSONString(phoneOutput), HttpStatus.OK);}}/*** 获取绑定状态* @param json* @return*/@RequestMapping(value = "/bind/{json}", method = RequestMethod.GET)public ResponseEntity<String> getBindStatu(@PathVariable String json) {PhoneUserInfo userInfo = JSON.toJavaObject(JSONObject.parseObject(json), PhoneUserInfo.class);PhoneUserInfo result = userInfoDao.getBindStatus(userInfo);if(result != null && result.getPhone() != null  && !result.getPhone().isEmpty()) {PhoneOutput output = new PhoneOutput(); output.setUserInfo(userInfo);output.setCount(1);return new ResponseEntity<String>(JSON.toJSONString(output), HttpStatus.OK);} else {return new ResponseEntity<String>("There is no result", HttpStatus.NOT_FOUND);}}
}

转载于:https://www.cnblogs.com/cunchen/p/9464139.html

【JAVA笔记——器】Spring MVC + HATEOAS RestFul快速搭建相关推荐

  1. 使用Spring MVC开发Restful Web服务

    REST简介 摘自Wikipedia: REST风格的体系结构由客户端和服务器组成. 客户端向服务器发起请求: 服务器处理请求并返回适当的响应. 请求和响应围绕资源表示的传递而构建. 资源本质上可以是 ...

  2. Spring MVC 学习总结(九)——Spring MVC实现RESTful与JSON(Spring MVC为前端提供服务)...

    Spring MVC 学习总结(九)--Spring MVC实现RESTful与JSON(Spring MVC为前端提供服务) 目录 一.JSON 1.1.概要 1.2.使用ModelAndView ...

  3. spring 笔记2:Spring MVC : Did not find handler method for 问题的解决

    spring 笔记2:Spring MVC : Did not find handler method for 问题的解决 参考文章: (1)spring 笔记2:Spring MVC : Did n ...

  4. spring boot / cloud (十七) 快速搭建注册中心和配置中心

    spring boot / cloud (十七) 快速搭建注册中心和配置中心 本文将使用spring cloud的eureka和config server来搭建. 然后搭建的模式,有很多种,本文主要聊 ...

  5. Spring MVC + Hibernate JPA + Bootstrap 搭建的博客系统

    Spring MVC + Hibernate JPA + Bootstrap 搭建的博客系统 Demo 相关阅读: 1.Spring MVC+Hibernate JPA+ Bootstrap 搭建的博 ...

  6. Java中级篇——Spring MVC 是什么(附加响应状态代码列举)

    1.关于springMVC 基于Spring框架,主要解决后端服务器接受客户端服务器接受客户提交的请求,并给予响应相关的问题.的框架 目录 1.关于springMVC 基于Spring框架,主要解决后 ...

  7. 搭建基于spring MVC框架 + RESTful架构风格技术总结

    2019独角兽企业重金招聘Python工程师标准>>> 实战篇: 在SpringMVC框架中搭建RESTful架构风格来完成客户端与服务器端的低耦合度.可扩展性.高并发与大数据流量的 ...

  8. Spring MVC 4 RESTFul Web Services CRUD例子(带源码)【这才是restful,超经典】

    [本系列其他教程正在陆续翻译中,点击分类:spring 4 mvc 进行查看.源码下载地址在文章末尾.] [翻译 by 明明如月 QQ 605283073] 原文地址:http://websystiq ...

  9. Java面试题--spring mvc

    什么是Spring MVC ?简单介绍下你对springMVC的理解? Spring MVC是一个基于MVC架构的用来简化web应用程序开发的应用开发框架,它是Spring的一个模块,无需中间整合层来 ...

  10. 如何用Java类配置Spring MVC(不通过web.xml和XML方式)

    DispatcherServlet是Spring MVC的核心,按照传统方式, 需要把它配置到web.xml中. 我个人比较不喜欢XML配置方式, XML看起来太累, 冗长繁琐. 还好借助于Servl ...

最新文章

  1. 图像降维之MDS特征抽取方法
  2. 0.1 hint crack
  3. Binder相关面试总结(三):Binder机制是如何跨进程的
  4. 15种CSS居中的方式
  5. angularJs动画 --实例
  6. Spark笔记:RDD基本操作(上)
  7. wifi rssi 计算 距离_WiFi和WLAN是一样的?真相在这里
  8. sharesdk短信验证码的集成
  9. 论文浅尝 | 用增强学习进行推理:问答与知识库完善(KBC)
  10. Centos7 下载、安装、配置、启动部署
  11. CATIA连接盘实体设计
  12. 大数据之-Hadoop_推荐系统框架图---大数据之hadoop工作笔记0015
  13. 用WebBrowser实现HTML界面的应用
  14. 国内几大主流论坛(bbs)系统
  15. git文件没有绿色红色小图标
  16. Spotfire调试经验——环比增长率的动态计算(Dynamic moving data percentage calculation in Spotfire visualization)
  17. python+appium出现selenium.common.exceptions.WebDriverException: Message: Method has not yet been imple
  18. 人工智能帮你文字转为Logo | 机器学习
  19. python常用快捷键mac_Mac PHPStorm 常用快捷键,常用设置
  20. Steam版XCOM: Enemy Within(内部敌人)不能启动的问题

热门文章

  1. Atitit 软件项目非法模块与功能的管理与  监狱管理的对比 原理与概论attilax总结
  2. atitit.按钮光标滑过高亮切换以及其他动态效果的实现css html js --attilax总结
  3. paip.python连接mysql最佳实践o4
  4. 为什么机器学习在投资领域不好使
  5. 阿里云混合云新一代运维平台的演进与实践
  6. 中铁总数据中心落户武清 项目总投资22.7亿元
  7. 【人脸识别】基于matlab GUI LBP人脸识别【含Matlab源码 1282期】
  8. 【图像分割】基于形态学重建和过滤改进FCM算法(FRFCM)的图像分割【含Matlab源码 085期】
  9. 【图像变换】基于matalb DIBR-3D图像变换【含Matalb源码 082期】
  10. mysql资源估算_关于数据库查询要耗费的服务器资源估算!高手进~