欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot中的Controller用法示例詳解

 更新時間:2023年06月25日 15:34:22   作者:蘋果牛頓吃  
Controller是SpringBoot里最基本的組件,他的作用是把用戶提交來的請求通過對URL的匹配,分配給不同的接收器,再進行處理,然后向用戶返回結(jié)果,這篇文章主要介紹了SpringBoot中的Controller用法,需要的朋友可以參考下

Controller

Controller是SpringBoot里最基本的組件,他的作用是把用戶提交來的請求通過對URL的匹配,分配給不同的接收器,再進行處理,然后向用戶返回結(jié)果。他的重點就在于如何從HTTP請求中獲得信息,提取參數(shù),并分發(fā)給不同的處理服務(wù)。

基本組成

一個最經(jīng)典的Controller應(yīng)該大概長這樣:

package com.example.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
 * Created by myths on 5/16/17.
 */
@Controller
public class IndexController {
    @RequestMapping(value = {"index", "/"})
    public String index(Model model) {
        return "index";
    }
}

復(fù)制

首先應(yīng)該在類的開頭,加上@Controller注解,告知Spring這是一個控制器。 然后在對應(yīng)的處理函數(shù)前面加上@RequestMapping,告知這個函數(shù)需要相應(yīng)的URL。 接著這個函數(shù)傳入了一個Model類型的參數(shù),這個參數(shù)主要是用于向模板傳遞數(shù)據(jù)。 該函數(shù)最后返回了一個叫"index"的字符串,表示將調(diào)用名為"index"的模板,具體名稱取決于模板引擎,比如對于jsp他將訪問"index.jsp"這個文件,對于thymeleaf,他將訪問"index.html"這個文件。

上面的是最簡單的用法,實際上有很多細(xì)節(jié)需要注意,下面就來一一解釋。

ResponseBody

如果我們想返回純字符串而不是渲染后的模板,那我們可以在需要返回字符串的函數(shù)前面加上@ResponseBody這個注解; 如果我們像對于整個類都返回字符串,我們也可以在這個類前面加上@ResponseBody注解,或者將@Controller注解換成@RestController,均可。

RequestMapping 基本用法

這個注解是用來告訴Spring,下面的這個函數(shù)或者類是用來映射到那個URL的,通常情況下有下面這些選項:

    @RequestMapping(
            path = {"/test"},
            params = {"name","userId"},
            method = {RequestMethod.GET},
            consumes = {"text/plain", "application/*"},
            produces = "text/plain",
            headers = "content-type=text/*"
    )

復(fù)制

我們可以指定這個路徑,參數(shù),方法,頭信息,來作為一個Controller的入口。當(dāng)然,通常我們只需要指定path就行了。

對象轉(zhuǎn)json或xml

這里有一個使用的小竅門,比如有時候我們希望返回json字符串,我們當(dāng)然可以調(diào)用jackson,gson,fastjson等等工具來組合數(shù)據(jù),但是這樣顯然比較麻煩。其實springboot自帶了將對象持久化的工具,只要我們在produces參數(shù)中指定頭信息,就可以將返回的對象直接轉(zhuǎn)換為json或xml。比如:

package com.mythsman.controller;
import com.mythsman.bean.TestBean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
    @RequestMapping(value = "test", produces = {"application/json;charset=UTF-8"})
    public TestBean test() {
        TestBean testBean = new TestBean();
        testBean.setName("myths");
        testBean.setAge(12);
        return testBean;
    }
}

復(fù)制

訪問后的返回結(jié)果就是{"name":"myths","age":12}。同理,也可以自動轉(zhuǎn)換成xml格式,不過xml格式對與map等的數(shù)據(jù)結(jié)構(gòu)無法支持,因此我們還是建議采用json。

作用對象

這個注解可以注解一個函數(shù),也可以注解一個類。當(dāng)注解一個類時,類中所有的方法都會在這個基礎(chǔ)上再進行過濾:

@Controller
@RequestMapping("/path1")
public class TestController {
    @RequestMapping("/path2")
    @ResponseBody
    public String index() {
        return "ok";
    }
}

復(fù)制

這個函數(shù)就能匹配"/path1/path1"這個地址。

缺省參數(shù)

當(dāng)RequestMapping的參數(shù)是空的時候,他就表示匹配剩余所有的頁面,實際上也就是匹配所有的404頁面。

@Controller
public class IndexController {
    @RequestMapping
    public String index(Model model) {
        return "index";
    }
}

復(fù)制

當(dāng)RequestMapping不指定參數(shù)名時,默認(rèn)就是path參數(shù)。

@Controller
public class IndexController {
    @RequestMapping("/index")
    public String index(Model model) {
        return "index";
    }
}

復(fù)制

PathVariable

RequestMapping中的path也可以是一個未定的變量,比如下面的代碼:

@Controller
public class TestController {
    @RequestMapping("/{variable}")
    @ResponseBody
    public String index(@PathVariable("variable")String variable) {
        return variable;
    }
}

復(fù)制

通過花括號我們就可以指定這個變量,并且通過@PathVariable注解得到這個參數(shù),非常方便。

RequestParam

RequestMapping也可以獲取類似表單傳回的信息形式,比如/index?name=XXX,這個name變量就可以通過RequestParam注解來獲得:

@Controller
public class TestController {
    @RequestMapping("/index")
    @ResponseBody
    public String index(@RequestParam(value = "name",required = false,defaultValue = "233")String name) {
        return name;
    }
}

復(fù)制

我們可以控制這個參數(shù)的名字,是否必須,以及設(shè)置默認(rèn)值,來更好的進行匹配。

RequestBody

RequestBody用來獲取Post信息的消息體,只要在參數(shù)中通過@RequestBody注解就能得到這個參數(shù):

@Controller
public class TestController {
    @RequestMapping("/index")
    @ResponseBody
    public String index(@RequestBody String body) {
		//對body進行解析即可
        return "success";
    }
}

到此這篇關(guān)于SpringBoot中的Controller用法的文章就介紹到這了,更多相關(guān)SpringBoot Controller用法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論