forked from springside/springside4
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Calvin
committed
Oct 11, 2014
1 parent
4a63d82
commit eb5018f
Showing
15 changed files
with
588 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>org.springside.examples</groupId> | ||
<artifactId>bootservice</artifactId> | ||
<version>4.3.0-SNAPSHOT</version> | ||
<packaging>war</packaging> | ||
<name>Springside :: Example :: SpringBoot WebService</name> | ||
|
||
<properties> | ||
<springside.version>4.3.0-SNAPSHOT</springside.version> | ||
<spring-boot.version>1.1.8.RELEASE</spring-boot.version> | ||
<h2.version>1.3.176</h2.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<!-- spring boot --> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-data-jpa</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-actuator</artifactId> | ||
</dependency> | ||
|
||
<!-- springside --> | ||
<dependency> | ||
<groupId>org.springside</groupId> | ||
<artifactId>springside-utils</artifactId> | ||
<version>${springside.version}</version> | ||
</dependency> | ||
|
||
<!-- database --> | ||
<dependency> | ||
<groupId>com.h2database</groupId> | ||
<artifactId>h2</artifactId> | ||
<version>${h2.version}</version> | ||
<scope>runtime</scope> | ||
</dependency> | ||
|
||
<!-- utils --> | ||
<dependency> | ||
<groupId>org.apache.commons</groupId> | ||
<artifactId>commons-lang3</artifactId> | ||
<version>3.2.1</version> | ||
</dependency> | ||
|
||
<!-- test --> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.assertj</groupId> | ||
<artifactId>assertj-core</artifactId> | ||
<version>1.5.0</version> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springside</groupId> | ||
<artifactId>springside-utils</artifactId> | ||
<version>${springside.version}</version> | ||
<classifier>tests</classifier> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
<version>${spring-boot.version}</version> | ||
<executions> | ||
<execution> | ||
<goals> | ||
<goal>repackage</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-war-plugin</artifactId> | ||
<configuration> | ||
<failOnMissingWebXml>false</failOnMissingWebXml> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
<dependencyManagement> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-dependencies</artifactId> | ||
<version>${spring-boot.version}</version> | ||
<type>pom</type> | ||
<scope>import</scope> | ||
</dependency> | ||
</dependencies> | ||
</dependencyManagement> | ||
|
||
|
||
</project> |
20 changes: 20 additions & 0 deletions
20
...bootservice/src/main/java/org/springside/examples/bootservice/BootServiceApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package org.springside.examples.bootservice; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
import org.springframework.context.annotation.ComponentScan; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
// Spring Java Config的标识 | ||
@Configuration | ||
@ComponentScan("org.springside.examples.bootservice") | ||
// Spring Boot的AutoConfig和载入外部properties文件的 标识 | ||
@EnableAutoConfiguration | ||
@EnableConfigurationProperties | ||
public class BootServiceApplication { | ||
|
||
public static void main(String[] args) throws Exception { | ||
SpringApplication.run(BootServiceApplication.class, args); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
examples/bootservice/src/main/java/org/springside/examples/bootservice/domain/IdEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2005, 2014 springside.github.io | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
*******************************************************************************/ | ||
package org.springside.examples.bootservice.domain; | ||
|
||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.GenerationType; | ||
import javax.persistence.Id; | ||
import javax.persistence.MappedSuperclass; | ||
|
||
/** | ||
* 统一定义id的entity基类. | ||
* | ||
* 基类统一定义id的属性名称、数据类型、列名映射及生成策略. | ||
* Oracle需要每个Entity独立定义id的SEQUCENCE时,不继承于本类而改为实现一个Idable的接口。 | ||
* | ||
* @author calvin | ||
*/ | ||
// JPA 基类的标识 | ||
@MappedSuperclass | ||
public abstract class IdEntity { | ||
|
||
protected Long id; | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
examples/bootservice/src/main/java/org/springside/examples/bootservice/domain/Task.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package org.springside.examples.bootservice.domain; | ||
|
||
import javax.persistence.Entity; | ||
import javax.persistence.JoinColumn; | ||
import javax.persistence.ManyToOne; | ||
import javax.persistence.Table; | ||
|
||
import org.apache.commons.lang3.builder.ToStringBuilder; | ||
|
||
// JPA实体类的标识 | ||
@Entity | ||
@Table(name = "ss_task") | ||
public class Task extends IdEntity { | ||
|
||
private String title; | ||
private String description; | ||
private User user; | ||
|
||
public String getTitle() { | ||
return title; | ||
} | ||
|
||
public void setTitle(String title) { | ||
this.title = title; | ||
} | ||
|
||
public String getDescription() { | ||
return description; | ||
} | ||
|
||
public void setDescription(String description) { | ||
this.description = description; | ||
} | ||
|
||
// 基于user_id列的多对一关系定义. | ||
@ManyToOne | ||
@JoinColumn(name = "user_id") | ||
public User getUser() { | ||
return user; | ||
} | ||
|
||
public void setUser(User user) { | ||
this.user = user; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return ToStringBuilder.reflectionToString(this); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
examples/bootservice/src/main/java/org/springside/examples/bootservice/domain/User.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package org.springside.examples.bootservice.domain; | ||
|
||
import javax.persistence.Entity; | ||
import javax.persistence.Table; | ||
|
||
import org.apache.commons.lang3.builder.ToStringBuilder; | ||
|
||
// JPA实体类的标识 | ||
@Entity | ||
@Table(name = "ss_user") | ||
public class User extends IdEntity { | ||
|
||
private String name; | ||
|
||
public User() { | ||
} | ||
|
||
public User(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return ToStringBuilder.reflectionToString(this); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...les/bootservice/src/main/java/org/springside/examples/bootservice/repository/TaskDao.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package org.springside.examples.bootservice.repository; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.data.repository.CrudRepository; | ||
import org.springside.examples.bootservice.domain.Task; | ||
|
||
/** | ||
* 基于Spring Data JPA的Dao接口。 | ||
*/ | ||
public interface TaskDao extends CrudRepository<Task, Long> { | ||
|
||
List<Task> findByUserId(Long id); | ||
} |
70 changes: 70 additions & 0 deletions
70
...ootservice/src/main/java/org/springside/examples/bootservice/rest/TaskRestController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package org.springside.examples.bootservice.rest; | ||
|
||
import java.net.URI; | ||
import java.util.List; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
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.ResponseStatus; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.web.util.UriComponentsBuilder; | ||
import org.springside.examples.bootservice.domain.Task; | ||
import org.springside.examples.bootservice.service.TaskService; | ||
import org.springside.modules.web.MediaTypes; | ||
|
||
// Spring MVC Controller的标识 | ||
@RestController | ||
@RequestMapping(value = "/task") | ||
public class TaskRestController { | ||
|
||
@Autowired | ||
private TaskService taskService; | ||
|
||
@RequestMapping(method = RequestMethod.GET, produces = MediaTypes.JSON_UTF_8) | ||
public List<Task> list() { | ||
return taskService.getAllTask(); | ||
} | ||
|
||
@RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = MediaTypes.JSON_UTF_8) | ||
public Task get(@PathVariable("id") Long id) { | ||
return taskService.getTask(id); | ||
} | ||
|
||
@RequestMapping(method = RequestMethod.POST, consumes = MediaTypes.JSON) | ||
public ResponseEntity<?> create(@RequestBody Task task, UriComponentsBuilder uriBuilder) { | ||
// 保存任务 | ||
taskService.saveTask(task); | ||
|
||
// 按照Restful风格约定,创建指向新任务的url, 也可以直接返回id或对象. | ||
Long id = task.getId(); | ||
HttpHeaders headers = createLocation(uriBuilder, "/task/" + id); | ||
|
||
return new ResponseEntity(headers, HttpStatus.CREATED); | ||
} | ||
|
||
private HttpHeaders createLocation(UriComponentsBuilder uriBuilder, String path) { | ||
URI uri = uriBuilder.path(path).build().toUri(); | ||
HttpHeaders headers = new HttpHeaders(); | ||
headers.setLocation(uri); | ||
return headers; | ||
} | ||
|
||
@RequestMapping(value = "/{id}", method = RequestMethod.PUT, consumes = MediaTypes.JSON) | ||
// 按Restful风格约定,返回204状态码, 无内容. 也可以返回200状态码. | ||
@ResponseStatus(HttpStatus.NO_CONTENT) | ||
public void update(@RequestBody Task task) { | ||
taskService.saveTask(task); | ||
} | ||
|
||
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE) | ||
@ResponseStatus(HttpStatus.NO_CONTENT) | ||
public void delete(@PathVariable("id") Long id) { | ||
taskService.deleteTask(id); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...es/bootservice/src/main/java/org/springside/examples/bootservice/service/TaskService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package org.springside.examples.bootservice.service; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.springside.examples.bootservice.domain.Task; | ||
import org.springside.examples.bootservice.repository.TaskDao; | ||
|
||
// Spring Bean的标识. | ||
@Component | ||
// 类中所有public函数都纳入事务管理的标识. | ||
@Transactional | ||
public class TaskService { | ||
|
||
@Autowired | ||
private TaskDao taskDao; | ||
|
||
public List<Task> getAllTask() { | ||
return (List<Task>) taskDao.findAll(); | ||
} | ||
|
||
public Task getTask(Long id) { | ||
return taskDao.findOne(id); | ||
} | ||
|
||
public void saveTask(Task task) { | ||
taskDao.save(task); | ||
} | ||
|
||
public void deleteTask(Long id) { | ||
taskDao.delete(id); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
examples/bootservice/src/main/resources/application-production.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
spring.jpa.database=H2 | ||
|
||
spring.datasource.driverClassName=org.h2.Driver | ||
spring.datasource.url=jdbc:h2:file:~/.h2/bootservice;AUTO_SERVER=TRUE;DB_CLOSE_DELAY=-1; | ||
spring.datasource.username=sa | ||
spring.datasource.password= | ||
|
||
spring.datasource.initialize=false | ||
|
||
spring.datasource.max-idle=10 | ||
spring.datasource.max-active=100 | ||
|
||
info.app.name=Spring Boot Web Service Example | ||
info.app.version=${project.version} | ||
|
||
|
Oops, something went wrong.