commit
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
+50
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
+19
@@ -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!";
|
||||
}
|
||||
|
||||
}
|
||||
+19
@@ -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!";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user