RESTful Service

By Spring boot

Q:RESTful Service的寫法很多種 , 為什麼要用Spring boot data rest?

  • 簡化Spring設定

  • 簡化程式

簡述Spring boot

  • 專門用來整合各種Spring Componment Framework , e.g. SpringMvc , Spring Data , Spring what ever...
  • 設計為可讓Application獨立運作 , 不用再依附其他如ApServer ...等
  • http://projects.spring.io/spring-boot/

官方教學

以最常會用的需求進行範例

撈資料 -> 出JSON

Q:用之前專案開發的寫法,要寫多少Class?

A : 大概最少要

  1. Entity Class
  2. Repository Class
  3. Manager Class
  4. Restful Controller Class
  5. Maybe some Interfaces
  6. UnitTest Class
  7. Config Class
  8. properties...
  9. ...

By Spring Boot

  • Entity Class

  • Restful Repo Class

  • 1 Config Class , 1 properties

pom.xml

<dependencyManagement>
        <dependencies>
            <dependency>
                <!-- Import dependency management from Spring Boot -->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>1.2.6.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
    </dependencies>
    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

Entity

@Entity
@Table(name = "HRUSER")
public class HrUser implements Serializable {

    private static final long serialVersionUID = 1L;
    @Size(max = 10)
    @Column(name = "CPNYID")
    private String cpnyid;
    @Id
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 20)
    @Column(name = "EMPID")
    private String empId;
    @Size(max = 7)
    @Column(name = "DEPT_NO")
    private String deptNo;
    @Size(max = 10)
    @Column(name = "POSSIE")
    private String possie;

    /*
     * ....以下省略
     */

}

Restful Repo

@RepositoryRestResource(collectionResourceRel = "user", path = "user")
public interface HrUserRepository
        extends PagingAndSortingRepository<HrUser, String> {

    HrUser findByLoginId(@Param("loginId")String loginId);

    HrUser findByEmpId(@Param("empId") String empId);

}

tip: @RepositoryRestResource()可以不加 , but spring boot 會很親切的幫你加上複數在path , 例如 user 變成 users

Config

@SpringBootApplication
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

App properties

# EMBEDDED SERVER CONFIGURATION (ServerProperties)
server.port=9876
# DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
spring.datasource.url=jdbc:sqlserver://
spring.datasource.username=
spring.datasource.password=
spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.SQLServer2008Dialect
spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.EJB3NamingStrategy
spring.jpa.show-sql=false

Run

console > mvn clean package
console > java -jar target/rest-data.jar

畫面

畫面

畫面

畫面

寫這麼少 , Spring 到底會出什麼東西?

  • 最基本的Restful Serivce 操作(CRUD)

  • 如果有寫額外的查詢 , 就會出額外的search method
  • 根據repo的類型出method(Page able or not)
  • 輸出格式:HAL(http://stateless.co/hal_specification.html)

後續....

其實Hr-Rest沒有用Spring boot

原因

  • 設定檔問題

  • 佈署整合問題

  • Log整合問題

anno-rest的實戰篇

補充

Code

說明

  • anno專案結構趨近於一般專案 , 一樣有anno-vo , repo , logic
  • rest專案直接dependency anno-logic
  • spring boot 會自動掃到repo的spring data interface
  • 因為rest出的資料不太一樣 , 所以額外寫了controller加工
  • application.properties採用外掛方式
  • anno-rest已經直接用到了1.3.0-BUILD-SNAPSHOT, 因為要解決Repo要整合QueryDSL的問題
  • spring boot 針對版本有的有嚴格檢查 , 例如spring data
public class App{

    public static void main(String[] args) throws Exception {
        System.getProperties()
            .setProperty("spring.config.location", 
                         ConfigPath.getDefaultPath()+"anno-rest.properties");
        SpringApplication.run(App.class, args);
    }

}

設定

# EMBEDDED SERVER CONFIGURATION (ServerProperties)
server.port=
server.context-path=/anno-rest
# DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
spring.datasource.url=jdbc:mariadb://____
spring.datasource.username=
spring.datasource.password=
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.datasource.testOnBorrow=true
spring.datasource.validationQuery=SELECT 1
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.EJB3NamingStrategy
spring.jpa.show-sql=false

Run

參考資料

Spring Boot RESTful

By Mars Yang

Spring Boot RESTful

  • 1,452