【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);
        }
    }
}
發佈了69 篇原創文章 · 獲贊 51 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章