Spring Boot 入門實戰:長鏈接轉短鏈接


  Spring Boot是由Pivotal團隊提供的全新框架,其設計目的是用來簡化新Spring應用的初始搭建以及開發過程。該框架使用了特定的方式來進行配置,從而使開發人員不再需要定義樣板化的配置。通過這種方式,Boot致力於在蓬勃發展的快速應用開發領域(rapid application development)成為領導者。(官方網址:http://projects.spring.io/spring-boot/

為了能快速了解Spring Boot,做了一個demo,是把用戶輸入的長鏈接轉為短鏈接,用戶訪問短鏈接可以重定向到長鏈接的功能!

開發工具:IntelliJ IDEA

開發環境:jdk1.8

 1 package demo;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.stereotype.Controller;
 6 import org.springframework.web.bind.annotation.PathVariable;
 7 import org.springframework.web.bind.annotation.RequestMapping;
 8 import org.springframework.web.bind.annotation.RequestMethod;
 9 import org.springframework.web.bind.annotation.ResponseBody;
10 
11 import javax.servlet.http.HttpServletRequest;
12 import java.util.HashMap;
13 import java.util.Map;
14 import java.util.Random;
15 
16 @Controller
17 @SpringBootApplication
18 public class Application {
19     private Map<String,String> urlMap = new HashMap<String,String>();
20     @RequestMapping("/")
21     public String index() {
22         return "/view/index.html";
23     }
24     @RequestMapping(value="/getShortURL",method= RequestMethod.POST)
25     @ResponseBody
26     public String getShortURL(String url) {
27         String runURL = addShortURL(url);
28         return "http://localhost:8080/url/"+runURL;
29     }
30     @RequestMapping(value="/url/{shortUrl}",method= RequestMethod.GET)
31     public String getRealURL(@PathVariable String shortUrl,HttpServletRequest request) {
32         String currentURL = request.getRequestURL().toString();
33 
34         return "redirect:"+urlMap.get(shortUrl);
35     }
36 
37     private synchronized String addShortURL(String url){
38         String rtnURL = getRandomStr();
39         if (urlMap.get(rtnURL)==null) {
40             urlMap.put(rtnURL,url);
41             return rtnURL;
42         }else{
43             return addShortURL(url);
44         }
45     }
46 
47     private String getRandomStr(){
48         //定義一個空字符串
49         String base = "abcdefghijklmnopqrstuvwxyz0123456789";
50         Random random = new Random();
51         StringBuffer sb = new StringBuffer();
52         for (int i = 0; i < 6; i++) {
53             int number = random.nextInt(base.length());
54             sb.append(base.charAt(number));
55         }
56         String rtnURL = sb.toString();
57         return rtnURL;
58     }
59 
60     public static void main(String[] args){
61         SpringApplication.run(Application.class, args);
62     }
63 
64 }

頁面代碼很簡單,一個輸入框,讓用戶輸入原本鏈接,點擊“Submit”!

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Title</title>
 6 </head>
 7 <body>
 8 <form action="/getShortURL"  method="post">
 9     <p>URL: <input type="text" name="url" /></p>
10     <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
11 </form>
12 </body>
13 </html>

 Spring Boot內嵌Apache服務器,直接右鍵“Run”,一個Web服務就啟動了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM