close

寫後台通常要用到Web Services的服務

這篇就介紹如何在SpringMVC上加上RESTFul Web Services

 

1. 首先開啟你的Webservice專案

創造一個要當作Webservice的檔案

01.png

 

2. 接著確保在你的servlet.xml上加入要被掃描的資料夾

02.png

 

3. 然後在檔案內容上宣告他是RestController

並且宣告他的Http類型

範例如下

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/service/example")
public class ExampleService {
 @RequestMapping(method = RequestMethod.GET)
 public @ResponseBody String getShopInJSON() {
  return "HELLO";
 }
}

03.png

 

4. 最後可以在畫面上看到結果就成功了

04.png  

arrow
arrow

    RX1226 發表在 痞客邦 留言(0) 人氣()