Spring Boot可以非常簡單的發布和調用RESTful web service,下面參考官方指導體驗一下
1.首先訪問 http://start.spring.io/ 生成Spring Boot基礎項目
或者使用git clone https://github.com/spring-guides/gs-rest-service.git
這里使用Maven導入IDEA如下:
此時項目已經可以啟動,但是沒有任何功能,可以看到啟動日志中嵌入tomcat的信息:
2.添加代碼:

@JsonIgnoreProperties(ignoreUnknown = true) public class Greeting { private final long id; private final String content; public Greeting(long id, String content) { this.id = id; this.content = content; } public long getId() { return id; } public String getContent() { return content; } }
---
@RestController public class GreetingController { private static final String template = "Hello, %s!"; private final AtomicLong counter = new AtomicLong(); //@CrossOrigin(origins = "http://localhost:8081") @RequestMapping("/greeting") public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) { System.out.println("getName:"+name); return new Greeting(counter.incrementAndGet(), String.format(template, name)); } }
---
此時再啟動,然后使用瀏覽器訪問 http://localhost:8080/greeting
瀏覽器顯示
{"id":1,"content":"Hello, World!"}
可見Spring使用Jackson JSON自動將Greeting對象轉為json
@RequestMapping注解將Http請求指向greeting()方法,可以通過@RequestMapping(method=GET)指定請求方式
@RequestParam注解將查詢參數name綁定到greeting方法的name參數上,並指定了默認值。
@RestController注解相當於
@Controller加
@ResponseBody的效果。
接下來是調用WebService,通過Spring提供的RestTemplate類
添加代碼:

public class Application { private static final Logger log = LoggerFactory.getLogger(Application.class); public static void main(String args[]) { RestTemplate restTemplate = new RestTemplate(); for (int k = 0 ; k< 10;k++) { Greeting greeting = restTemplate.getForObject("http://localhost:8080/greeting?name=luangeng"+k, Greeting.class); System.out.println("getName "+greeting.toString()); } } }
---
使用AJAX調用WebService
寫一個簡單的Html頁面:
<!DOCTYPE html> <html> <head> <title>ws test</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> </head> <body> <div> <p class="greeting-id">The ID is: </p> <p class="greeting-content">The content is: </p> </div> </body> </html> <script> $().ready(function() { $.ajax({ url: "http://localhost:8080/greeting?name=luangeng" }).then(function(data) { $('.greeting-id').append(data.id); $('.greeting-content').append(data.content); }); }); </script>
---
添加到jetty的webapp中,在8081端口啟動jetty :java -jar ../start.jar jetty.http.port=8081 ,使用瀏覽器訪問 http://localhost:8081/test.html 可看到瀏覽器控制台如下報錯信息:
可知報錯原因為8080拒絕訪問,8080和8081端口不同源導致的。
打開跨域訪問:在greeting方法上加@CrossOrigin(origins = "http://localhost:8081")注解即可
再次嘗試即可看到瀏覽器界面顯示如下:
說明調用WS成功。
通過以上Spring官網的幾個簡單的例子可以看到使用SpringBoot發布和調用RESTful WebService是非常容易的。
常用注解:
@RestController :避免了每個方法都要添加@ResponseBody注解。@RestController 內部包含@ResponseBody注解,可以認為是 @Controller 和 @ResponseBody的組合。
@RequestBody : 如果方法參數被 @RequestBody注解,Spring將綁定HTTP請求體到那個參數上。Spring將根據請求中的ACCEPT或者 Content-Type header使用 HTTP Message converters 來將http請求體轉化為domain對象。
@ResponseBody : 如果方法加上了@ResponseBody注解,Spring返回值到響應體。Spring將根據請求中的 Content-Type header(私下)使用 HTTP Message converters 來將domain對象轉換為響應體。
ResponseEntity: HTTP響應(response)對象,可以指定狀態碼、頭信息和響應體。包含要構建的HTTP Response 的全部信息。
@PathVariable: 參數注解,表示方法參數綁定到一個url變量中
end