Skip to content

Commit

Permalink
update boot-service to spring-boot 1.2.1 and more simple
Browse files Browse the repository at this point in the history
also add h2 console to access embedded H2 in development env.
  • Loading branch information
calvin1978 committed Jan 8, 2015
1 parent 6a788aa commit df81d06
Show file tree
Hide file tree
Showing 16 changed files with 114 additions and 169 deletions.
1 change: 1 addition & 0 deletions examples/boot-more/README.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
What is more?

1. Use Jetty instead of Tomcat
2. Don't use spring boot parent
2. Use JavaSimon, demo AOP and Servlet mapping definition
49 changes: 7 additions & 42 deletions examples/boot-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@
<packaging>war</packaging>
<name>Springside :: Example :: SpringBoot WebService</name>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.1.RELEASE</version>
</parent>

<properties>
<springside.version>4.3.0-SNAPSHOT</springside.version>
<spring-boot.version>1.1.10.RELEASE</spring-boot.version>
<commons-lang3.version>3.2.1</commons-lang3.version>
<assertj.version>1.7.0</assertj.version>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.6</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<java.version>1.7</java.version>
</properties>

<dependencies>
Expand All @@ -28,7 +30,6 @@
<artifactId>spring-boot-starter-web</artifactId>
</dependency>


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
Expand All @@ -49,7 +50,6 @@
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>

<!-- utils -->
Expand Down Expand Up @@ -93,42 +93,7 @@
<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>
<version>2.5</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>

<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.springside.examples.bootservice.config;

public class Profiles {

public static final String PRODUCTION = "production";

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.springside.examples.bootservice.config;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.embedded.ServletContextInitializer;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

@Configuration
public class WebConfigurer implements ServletContextInitializer {

@Autowired
private Environment env;

@Override
public void onStartup(ServletContext servletContext)
throws ServletException {
if (!env.acceptsProfiles(Profiles.PRODUCTION)) {
initH2Console(servletContext);
}
}

private void initH2Console(ServletContext servletContext) {
ServletRegistration.Dynamic h2ConsoleServlet = servletContext
.addServlet("H2Console", new org.h2.server.web.WebServlet());
h2ConsoleServlet.addMapping("/h2/*");
h2ConsoleServlet.setInitParameter("-properties", "src/main/resources");
h2ConsoleServlet.setLoadOnStartup(1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,15 @@
* 统一定义id的entity基类.
*
* 基类统一定义id的属性名称、数据类型、列名映射及生成策略.
* Oracle需要每个Entity独立定义id的SEQUCENCE时,不继承于本类而改为实现一个Idable的接口
* 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;
}
public Long id;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,13 @@
@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;
}
public String title;
public String description;

// 基于user_id列的多对一关系定义.
@ManyToOne
@JoinColumn(name = "user_id")
public User getUser() {
return user;
}

public void setUser(User user) {
this.user = user;
}
public User user;

@Override
public String toString() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
@Table(name = "ss_user")
public class User extends IdEntity {

private String name;
public String name;

public User() {
}
Expand All @@ -19,14 +19,6 @@ 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import java.util.List;

import org.springframework.data.repository.CrudRepository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springside.examples.bootservice.domain.Task;

/**
* 基于Spring Data JPA的Dao接口。
*/
public interface TaskDao extends CrudRepository<Task, Long> {
public interface TaskDao extends JpaRepository<Task, Long> {

List<Task> findByUserId(Long id);
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class TaskRestController {

@RequestMapping(method = RequestMethod.GET, produces = MediaTypes.JSON_UTF_8)
public List<Task> list() {
counterService.increment("web.task.list");
counterService.increment("task.list");
return taskService.getAllTask();
}

Expand All @@ -43,27 +43,28 @@ public Task get(@PathVariable("id") Long id) {
}

@RequestMapping(method = RequestMethod.POST, consumes = MediaTypes.JSON)
public ResponseEntity<?> create(@RequestBody Task task, UriComponentsBuilder uriBuilder) {
public ResponseEntity<?> create(@RequestBody Task task,
UriComponentsBuilder uriBuilder) {
counterService.increment("task.create");
// 保存任务
taskService.saveTask(task);

// 按照Restful风格约定,创建指向新任务的url, 也可以直接返回id或对象.
Long id = task.getId();
HttpHeaders headers = createLocation(uriBuilder, "/task/" + id);
// 按照Restful风格约定, 创建指向新任务的url, 也可以直接返回id或对象.
HttpHeaders headers = createLocation(uriBuilder, "/task/" + task.id);

return new ResponseEntity(headers, HttpStatus.CREATED);
}

private HttpHeaders createLocation(UriComponentsBuilder uriBuilder, String path) {
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状态码.
// 按Restful风格约定, 返回204状态码, 无内容, 也可以返回200状态码.
@ResponseStatus(HttpStatus.NO_CONTENT)
public void update(@RequestBody Task task) {
counterService.increment("task.update");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#H2 Server Properties
#Fri Jan 09 00:37:05 CST 2015
0=TestDB(Embedded)|org.h2.Driver|jdbc\:h2\:mem\:testdb|sa
webAllowOthers=false
webPort=8082
webSSL=false
19 changes: 3 additions & 16 deletions examples/boot-service/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,27 +1,14 @@
# server
# server settings
server.port=8080
management.port=7002

# logging
logging.file=/tmp/logs/bootservice.log

# pretty json format
http.mappers.json-pretty-print=true
http.mappers.json-sort-keys=true
spring.jackson.serialization.INDENT_OUTPUT=true
spring.datasource.sqlScriptEncoding=UTF-8

# disable spring boot strange behavior
spring.main.show-banner=false
spring.jpa.hibernate.ddl-auto=none
spring.jpa.showsql=false

#disable useless endpoints
endpoints.autoconfig.enabled=false
endpoints.beans.enabled=false
endpoints.configprops.enabled=false
endpoints.mappings.enabled=false
endpoints.trace.enabled=false
#endpoints.shutdown.enabled=true

# /info endpoint
info.app.name=Spring Boot Web Service Example
info.app.version=${project.version}
5 changes: 0 additions & 5 deletions examples/boot-service/src/main/resources/logback.xml

This file was deleted.

30 changes: 30 additions & 0 deletions examples/boot-service/src/main/webapp/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!DOCTYPE html>
<html>
<head>
<title>Spring Boot Web Service Example</title>
</head>
<body>
<p>Access below management endpoint:
<ul>
<li><a href="http://localhost:7002/health">http://localhost:7002/health</a></li>
<li><a href="http://localhost:7002/info">http://localhost:7002/info</a></li>
<li><a href="http://localhost:7002/dump">http://localhost:7002/dump</a></li>
<li><a href="http://localhost:7002/metrics">http://localhost:7002/metrics</a></li>
<li><a href="http://localhost:7002/env">http://localhost:7002/env</a></li>
<li>shutdown(disable by default, POST method)</li></ul>
</p>

<p>JMX expose as Restful by jolokia. e.g. Tomcat's MBean:
<ul>
<li><a href="http://localhost:7002/jolokia/read/Tomcat:type=Connector,port=8080">http://localhost:7002/jolokia/read/Tomcat:type=Connector,port=8080</a></li>
</ul>
</p>

<p>H2 Console in development profile:
<ul>
<li><a href="http://localhost:8080/h2">http://localhost:8080/h2/</a></li>
</ul>
</p>

</body>
</html>
Loading

0 comments on commit df81d06

Please sign in to comment.