Skip to content

Commit

Permalink
springside#394 update Spring Boot demo
Browse files Browse the repository at this point in the history
  • Loading branch information
Calvin committed Oct 11, 2014
1 parent eb5018f commit 1073c07
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 40 deletions.
84 changes: 54 additions & 30 deletions examples/bootservice/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
<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>
Expand Down Expand Up @@ -41,7 +40,6 @@
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>${h2.version}</version>
<scope>runtime</scope>
</dependency>

Expand All @@ -52,20 +50,26 @@
<version>3.2.1</version>
</dependency>

<!-- jmx -->
<dependency>
<groupId>org.jolokia</groupId>
<artifactId>jolokia-core</artifactId>
</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>
<version>1.6.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springside</groupId>
<artifactId>springside-utils</artifactId>
Expand All @@ -75,29 +79,51 @@
</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>
<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-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>

<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>

<dependencyManagement>
<dependencies>
Expand All @@ -110,6 +136,4 @@
</dependency>
</dependencies>
</dependencyManagement>


</project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@

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文件的 标识
// Spring Boot的AutoConfig
@EnableAutoConfiguration
@EnableConfigurationProperties
public class BootServiceApplication {

public static void main(String[] args) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*------------------------------------------------------------------------------
* COPYRIGHT Ericsson 2014
*
* The copyright to the computer program(s) herein is the property of
* Ericsson Inc. The programs may be used and/or copied only with written
* permission from Ericsson Inc. or in accordance with the terms and
* conditions stipulated in the agreement/contract under which the
* program(s) have been supplied.
*----------------------------------------------------------------------------*/
package org.springside.examples.bootservice.rest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.metrics.CounterService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class IndexRestController {

@Autowired
private CounterService counterService;

@RequestMapping(value = "/", produces = "text/html")
public String index() {
counterService.increment("index");
return "<html><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>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>" + "</body></html>";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.metrics.CounterService;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -23,21 +24,27 @@
@RequestMapping(value = "/task")
public class TaskRestController {

@Autowired
private CounterService counterService;

@Autowired
private TaskService taskService;

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

@RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = MediaTypes.JSON_UTF_8)
public Task get(@PathVariable("id") Long id) {
counterService.increment("task.get");
return taskService.getTask(id);
}

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

Expand All @@ -59,12 +66,14 @@ private HttpHeaders createLocation(UriComponentsBuilder uriBuilder, String path)
// 按Restful风格约定,返回204状态码, 无内容. 也可以返回200状态码.
@ResponseStatus(HttpStatus.NO_CONTENT)
public void update(@RequestBody Task task) {
counterService.increment("task.update");
taskService.saveTask(task);
}

@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
@ResponseStatus(HttpStatus.NO_CONTENT)
public void delete(@PathVariable("id") Long id) {
counterService.increment("task.delete");
taskService.deleteTask(id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,3 @@ 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}


Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ management.port=7002

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

# pretty json format
http.mappers.json-pretty-print=true
http.mappers.json-sort-keys=true

Expand All @@ -11,11 +12,13 @@ spring.main.show-banner=false
spring.jpa.hibernate.ddl-auto=none
spring.jpa.showsql=false

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

info.app.name=Spring Boot Web Service Example
info.app.version=${project.version}

0 comments on commit 1073c07

Please sign in to comment.