• 【JAVA笔记——器】Spring MVC + HATEOAS RestFul快速搭建


    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.xsd
               http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd
               http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
               http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
               http://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);
            }
        }
    }
    
  • 相关阅读:
    哈尔滨理工大学第六届程序设计团队 H-Permutation
    哈尔滨理工大学第六届程序设计团队 E-Mod
    哈尔滨理工大学第六届程序设计团队 I-Team
    HDU Today
    最短路
    Pseudoforest(伪最大生成树)
    Connect the Cities(prim)用prim都可能超时,交了20几发卡时过的
    Jungle Roads(最小生成树)
    linux读取yaml文件的某个属性值
    dos查看电脑配置
  • 原文地址:https://www.cnblogs.com/cunchen/p/9464136.html
Copyright © 2020-2023  润新知