SpringBoot的搭建筆記

1.先建立項目
項目建立后隙券,在application.properties配置端口:

server.port= 80

創(chuàng)建Control類:

@RestController
@RequestMapping("/haha")
public class TestControl {

    @RequestMapping(path = {"/forever"})
    public String HelloSpring(){
        return "{data:hahaforever111}";
    }

}

開啟服務(wù)胯甩,輸入地址:http://localhost/haha/forever客扎,訪問到數(shù)據(jù)。

2.集成mySql數(shù)據(jù)庫:
在pom.xml里添加依賴:

<dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

在application.properties配置數(shù)據(jù)庫信息:

spring.datasource.url=jdbc:mysql://localhost:3306/myuserinfos?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456

訪問數(shù)據(jù)庫代碼:

@Autowired
    private JdbcTemplate jdbcTemplate;

    @RequestMapping("/getForever")
    public String getDbType() {
        String sql = "select * from user_info_haha";
        Map a = (Map) jdbcTemplate.queryForList(sql).get(0);
        return "{data:\""+a.get("name").toString()+"\"}";
    }

3.傳參方式:

@RequestMapping(path = {"/getParamDemo1/{id}"})
    public String getParamDemo1 (@PathVariable("id") int userId){
        System.out.println("get param " + userId);
        return "success get param "+userId;
    }

@RequestMapping(path = {"/getParamDemo2"} ,method = RequestMethod.GET)
    public String getParamDemo2 (@RequestParam(value="param1",required = false) int param){
        System.out.println("get param " + param);
        return "success get param "+param;
    }

4.調(diào)用外部接口:

import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

@Service
public class MTestClient {
    public static String sendPostRequest(String url, MultiValueMap<String, String> params){
        RestTemplate client = new RestTemplate();
        //新建Http頭步做,add方法可以添加參數(shù)
        HttpHeaders headers = new HttpHeaders();
        //設(shè)置請求發(fā)送方式
        HttpMethod method = HttpMethod.POST;
        // 以表單的方式提交
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        //將請求頭部和參數(shù)合成一個請求
        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(params, headers);
        //執(zhí)行HTTP請求卡乾,將返回的結(jié)構(gòu)使用String 類格式化(可設(shè)置為對應(yīng)返回值格式的類)
        ResponseEntity<String> response = client.exchange(url, method, requestEntity,String .class);

        return response.getBody();
    }

}

5.顯示html頁:
在templates下添加html文件,
在application.properties下添加代碼:

spring.mvc.view.prefix=classpath:/templates/
spring.mvc.view.suffix=.html

在pom.xml中添加依賴:

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

調(diào)用控制器:

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/ht")
public class HtmlControl {

    @RequestMapping("/hhh")
    public String toHtml(){
            return "my_test_htfile"; //當(dāng)瀏覽器輸入/hhh信卡,會返回 /templates/my_test_htfile.html頁面
        }
}

6.打包
繼承SpringBootServletInitializer類隔缀,實現(xiàn)configure方法:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Override//為了打包springboot項目
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(this.getClass());
    }
}

在pom下添加代碼:

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.lml.demo.DemoApplication</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>

選中clean complie package 點擊運行:


image.png

最后在target下找到j(luò)ar包,使用 java -jar xxx.jar命令開啟服務(wù)


image.png

7.添加ssl證書:
先把證書文件拷貝到resources下傍菇,在application.properties下添加信息:

server.ssl.key-store=classpath:XXX.jks
server.ssl.key-store-password=XXXXXX
server.ssl.keyStoreType=JKS
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末猾瘸,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子丢习,更是在濱河造成了極大的恐慌牵触,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,539評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件咐低,死亡現(xiàn)場離奇詭異揽思,居然都是意外死亡,警方通過查閱死者的電腦和手機渊鞋,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,594評論 3 396
  • 文/潘曉璐 我一進店門绰更,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人锡宋,你說我怎么就攤上這事儡湾。” “怎么了执俩?”我有些...
    開封第一講書人閱讀 165,871評論 0 356
  • 文/不壞的土叔 我叫張陵徐钠,是天一觀的道長。 經(jīng)常有香客問我役首,道長尝丐,這世上最難降的妖魔是什么显拜? 我笑而不...
    開封第一講書人閱讀 58,963評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮爹袁,結(jié)果婚禮上远荠,老公的妹妹穿的比我還像新娘。我一直安慰自己失息,他們只是感情好譬淳,可當(dāng)我...
    茶點故事閱讀 67,984評論 6 393
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著盹兢,像睡著了一般邻梆。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上绎秒,一...
    開封第一講書人閱讀 51,763評論 1 307
  • 那天浦妄,我揣著相機與錄音,去河邊找鬼见芹。 笑死剂娄,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的辆童。 我是一名探鬼主播宜咒,決...
    沈念sama閱讀 40,468評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼把鉴!你這毒婦竟也來了故黑?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,357評論 0 276
  • 序言:老撾萬榮一對情侶失蹤庭砍,失蹤者是張志新(化名)和其女友劉穎场晶,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體怠缸,經(jīng)...
    沈念sama閱讀 45,850評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡诗轻,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,002評論 3 338
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了揭北。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片扳炬。...
    茶點故事閱讀 40,144評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖搔体,靈堂內(nèi)的尸體忽然破棺而出恨樟,到底是詐尸還是另有隱情,我是刑警寧澤疚俱,帶...
    沈念sama閱讀 35,823評論 5 346
  • 正文 年R本政府宣布劝术,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏养晋。R本人自食惡果不足惜衬吆,卻給世界環(huán)境...
    茶點故事閱讀 41,483評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望绳泉。 院中可真熱鬧逊抡,春花似錦、人聲如沸零酪。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,026評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽蛾娶。三九已至,卻和暖如春潜秋,著一層夾襖步出監(jiān)牢的瞬間蛔琅,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,150評論 1 272
  • 我被黑心中介騙來泰國打工峻呛, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留罗售,地道東北人。 一個月前我還...
    沈念sama閱讀 48,415評論 3 373
  • 正文 我出身青樓钩述,卻偏偏與公主長得像寨躁,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子牙勘,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,092評論 2 355

推薦閱讀更多精彩內(nèi)容