This commit is contained in:
sxyx2008
2014-07-01 16:26:32 +08:00
parent 6a08435941
commit 4b306d453b
18 changed files with 280 additions and 1 deletions
+3 -1
View File
@@ -1,3 +1,5 @@
/.project
/.settings
/src
/src
/.idea
/*.iml
+1
View File
@@ -12,5 +12,6 @@
<module>spring3-jmx-example</module>
<module>spring3-rmi-example</module>
<module>spring3-rmi-httpinvoker-example</module>
<module>spring-boot-example</module>
</modules>
</project>
+3
View File
@@ -0,0 +1,3 @@
/spring-boot-example.iml
/target
/*.iml
+14
View File
@@ -0,0 +1,14 @@
spring-boot-example
=======================
使用```spring boot```和```spring hateoas```发布基于```REST```风格的接口
运行```Application```中的```main```方法。然后浏览器访问
* [http://localhost:8080/](http://localhost:8080/) http://localhost:8080/ SampleController中的home()
* [http://localhost:8080/greeting](http://localhost:8080/greeting) http://localhost:8080/greeting GreetingController中的greeting()方法
* [http://localhost:8080/print/zhangsan](http://localhost:8080/print/zhangsan) http://localhost:8080/print/{name} GreetingController中的print()方法
* [http://localhost:8080/hello/zhangsan/18](http://localhost:8080/hello/zhangsan/18) http://localhost:8080/hello/{name}/{age} SimpleSpringBoot中的hello()方法
+105
View File
@@ -0,0 +1,105 @@
<?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">
<!--
<parent>
<artifactId>maven-framework-project</artifactId>
<groupId>net.aimeizi</groupId>
<version>1.0.0</version>
</parent>
-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.1.3.RELEASE</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-boot-example</artifactId>
<dependencies>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.hateoas</groupId>
<artifactId>spring-hateoas</artifactId>
<version>0.12.0.RELEASE</version>
</dependency>
</dependencies>
<properties>
<start-class>net.aimeizi.Application</start-class>
</properties>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>http://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>http://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,16 @@
package net.aimeizi.springboot.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
/**
* Created by 冯靖 on 14-3-7.
*/
@ComponentScan
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
@@ -0,0 +1,20 @@
package net.aimeizi.springboot.example;
import org.springframework.hateoas.ResourceSupport;
/**
* 编写实体类继承org.springframework.hateoas.ResourceSupport
* Created by 冯靖 on 14-3-7.
*/
public class Greeting extends ResourceSupport{
private final String content;
public Greeting(String content) {
this.content = content;
}
public String getContent() {
return content;
}
}
@@ -0,0 +1,50 @@
package net.aimeizi.springboot.example;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn;
/**
* 使用spring-hateoas创建RESTful Web Service
* 1.编写实体类继承org.springframework.hateoas.ResourceSupport
* 2.编写一个返回类型为HttpEntity<T>的方法
* 3.使用org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo方法
* Created by 冯靖 on 14-3-7.
*/
@Controller
public class GreetingController {
private static final String template = "Hello, %s!";
//private final AtomicLong counter = new AtomicLong();
@RequestMapping(value = "/greeting",method = {RequestMethod.GET})
@ResponseBody
public HttpEntity<Greeting> greeting(
@RequestParam(value="name", required=false, defaultValue="World") String name) {
Greeting greeting = new Greeting(String.format(template, name));
greeting.add(linkTo(methodOn(GreetingController.class).greeting(name)).withSelfRel());
return new ResponseEntity<Greeting>(greeting, HttpStatus.OK);
}
@RequestMapping(value = "/print/{name}",method = {RequestMethod.GET})
@ResponseBody
public HttpEntity<Greeting> print(@PathVariable(value = "name")String name){
Greeting greeting=new Greeting(String.format("%s,i will be back!",name));
greeting.add(linkTo(methodOn(GreetingController.class).print(name)).withSelfRel());
return new ResponseEntity<Greeting>(greeting,HttpStatus.OK);
}
}
@@ -0,0 +1,19 @@
package net.aimeizi.springboot.example;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* Created by 冯靖 on 14-3-7.
*/
@Controller
public class SampleController {
@RequestMapping("/")
@ResponseBody
String home() {
return "Hello World!";
}
}
@@ -0,0 +1,19 @@
package net.aimeizi.springboot.example;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* Created by 冯靖 on 14-3-7.
*/
@Controller
public class SimpleSpringBoot {
@RequestMapping("/hello/{name}/{age}")
@ResponseBody
String hello(@PathVariable String name,@PathVariable("age") int age) {
return "Hello, "+name+"! You Are "+age+" years old!";
}
}
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!--
<contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator">
<resetJUL>true</resetJUL>
</contextListener>
-->
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d %5p %40.40c:%4L - %m%n</pattern>
</encoder>
</appender>
<logger name="org.springsource.restbucks" level="info" />
<logger name="org.springframework" level="info" />
<root level="warn">
<appender-ref ref="console" />
</root>
</configuration>
+1
View File
@@ -2,3 +2,4 @@
/.settings
/target
/.classpath
/*.iml
+1
View File
@@ -2,3 +2,4 @@
/.settings
/target
/.classpath
/*.iml
@@ -2,3 +2,4 @@
/.settings
/target
/.classpath
/*.iml
+1
View File
@@ -2,3 +2,4 @@
/.classpath
/.project
/.settings
/*.iml
+1
View File
@@ -4,3 +4,4 @@
/.gradle
/.settings
/build
/*.iml
+1
View File
@@ -2,3 +2,4 @@
/.settings
/target
/.classpath
/*.iml
@@ -3,3 +3,4 @@
/.settings
/.classpath
/.project
/*.iml