Controller的不同寫法和相應註解

寫法一:

@RestController
@RequestMapping("/advertise")
public class AdvertiseController {
	@CrossOrigin
	@RequestMapping(value = "/add/{id}", method = RequestMethod.POST)
	public JSONObject addAdvertise(@RequestBody JSONObject params, @Context HttpServletRequest request, @PathVariable("id") String id) {

1.@PathVariable:
通過@PathVariable可以將URL中佔位符參數綁定到控制器處理方法的入參中:URL中的{xxx}佔位符可以通過@PathVariable(“xxx”)綁定到操作方法的入參中。

2.@RequestBody:
將請求體中的整體數據轉化爲對象,GET方式無請求體,所以使用@RequestBody接收數據時,前端不能使用GET方式提交數據,而是用POST方式進行提交。

3.@Context:
不太瞭解這個註解,但是我發現把這個註解去掉也不影響啥。

@RestController
public class HuiController {

    @RequestMapping(value="huitest")
    public String hui(@RequestBody JSONObject params, HttpServletRequest request) {
    	String qiang = request.getParameter("hehe");
    	String xiao = params.getString("qiang");
    	return qiang + "<-->" + xiao;
    }
}

在這裏插入圖片描述
也可以用這個curl命令測試:

curl http://localhost:8080/huitest -X POST -d '{"qiang":"qie"}' --header "Content-Type: application/json"

4.@CrossOrigin:
是用來處理跨域請求的註解

跨域,指的是瀏覽器不能執行其他網站的腳本。它是由瀏覽器的同源策略造成的,是瀏覽器對JavaScript施加的安全限制。
所謂同源是指,域名,協議,端口均相同,不明白沒關係,舉個栗子:
http://www.123.com/index.html 調用 http://www.123.com/server.PHP (非跨域)
http://www.123.com/index.html 調用 http://www.456.com/server.php (主域名不同:123/456,跨域)
http://abc.123.com/index.html 調用 http://def.123.com/server.php (子域名不同:abc/def,跨域)
http://www.123.com:8080/index.html 調用 http://www.123.com:8081/server.php (端口不同:8080/8081,跨域)
http://www.123.com/index.html 調用 https://www.123.com/server.php (協議不同:http/https,跨域)
請注意:localhost和127.0.0.1雖然都指向本機,但也屬於跨域。
瀏覽器執行javascript腳本時,會檢查這個腳本屬於哪個頁面,如果不是同源頁面,就不會被執行。
當域名www.abc.com下的js代碼去訪問www.def.com域名下的資源,就會受到限制。
@CrossOrigin可以處理跨域請求,讓你能訪問不是一個域的文件。

5.@RequestMapping:
配置url映射,此註解即可以作用在控制器的某個方法上,也可以作用在此控制器類上。
當控制器在類級別上添加@RequestMapping註解時,這個註解會應用到控制器的所有處理器方法上。處理器方法上的@RequestMapping註解會對類級別上的@RequestMapping的聲明進行補充。
例子一:@RequestMapping僅作用在處理器方法上

@RestController
public class HelloController {

    @RequestMapping(value="/hello",method= RequestMethod.GET)
    public String sayHello(){
        return "hello";
    }
}

以上代碼sayHello所響應的url=localhost:8080/hello。
例子二:@RequestMapping僅作用在類級別上

@RestController
@RequestMapping("/hello")
public class HelloController {

    @RequestMapping(method= RequestMethod.GET)
    public String sayHello(){
        return "hello";
    }
}

以上代碼sayHello所響應的url=localhost:8080/hello,效果與例子一一樣,沒有改變任何功能。
例子三:@RequestMapping作用在類級別和處理器方法上

@RestController
@RequestMapping("/hello")
public class HelloController {

    @RequestMapping(value="/sayHello",method= RequestMethod.GET)
    public String sayHello(){
        return "hello";
    }
    @RequestMapping(value="/sayHi",method= RequestMethod.GET)
    public String sayHi(){
        return "hi";
    }
}

這樣,以上代碼中的sayHello所響應的url=localhost:8080/hello/sayHello。
sayHi所響應的url=localhost:8080/hello/sayHi。
@RequestMapping 和 @GetMapping @PostMapping 區別:
@GetMapping是一個組合註解,是@RequestMapping(method = RequestMethod.GET)的縮寫。
@PostMapping是一個組合註解,是@RequestMapping(method = RequestMethod.POST)的縮寫。
如果@RequestMapping不指定請求方式則兩種請求方式都可以。

@RestController
public class HuiController {

    @RequestMapping("huitest")
//	@PostMapping("huitest") 和 @RequestMapping(value="huitest",method = RequestMethod.POST)等效
//	@GetMapping(value="huitest") 和 @RequestMapping(value="huitest",method = RequestMethod.GET)等效
    public String hui(HttpServletRequest request) {
    	String hui = request.getParameter("hehe");
    	return hui;
    }
}

在這裏插入圖片描述
用瀏覽器直接訪問其實是GET請求
在這裏插入圖片描述

6.@RestController:
在spring4.0之後,引入了@RestController這個註解。這個註解相當於把@ResponseBody + @Controller合在一起。
我們先來看一下@RestController的源碼:

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Controller
@ResponseBody
public @interface RestController {
    String value() default "";
}

前端在通過表單提交和a標籤請求後端的時候只需要註解@Controller即可,但是在通過Ajax請求後端的時候,還需要增加一個@ResponseBody註解,但是在spring4.0之後,無論是表單還是a標籤還是Ajax請求後端,都只要@RestController一個註解即可。

@ResponseBody能將對象自動轉換爲json字符串,所以以往在Ajax返回時直接返回一個對象就行,而在表單和a標籤請求是返回需要調用JSON.toJSONString(object)這個方法將對象轉換爲json字符串。

在spring4.0後,所有請求在@RestController之下,直接返回對象即可,再也不需要轉換爲json字符串了。

@RestController
public class HelloController {

    @RequestMapping(value="/hello",method= RequestMethod.GET)
    public String sayHello(){
        return "hello";
    }
}

與下面的代碼作用一樣

@Controller
@ResponseBody
public class HelloController {

    @RequestMapping(value="/hello",method= RequestMethod.GET)
    public String sayHello(){
        return "hello";
    }
}

使用了@RestController之後,所有返回的數據他都會把你解析爲json字符串,所以如果是前後臺不分開式開發(即返回的是跳轉頁面名字)時,不能使用@RestContoller,不然只會在原來頁面中顯式你要跳轉頁面的名字!
可參考:http://www.ityouknow.com/springboot/2016/02/03/spring-boot-web.html
注意:@Contoller註解在springboot1.5以前的版本中在maven配置spring-boot-starter-web(@Contoller引入的包是org.springframework.stereotype.Controller)中有,而1.5及以後則沒有了。
 
寫法二(其實就是Jersey寫Restful接口):

@Path("/admin")
@Component
public class ManagementResource {
	@POST
	@Path("/login")
	@Consumes(MediaType.APPLICATION_JSON)
	@Produces(MediaType.APPLICATION_JSON)
	@CrossOrigin
	public Response login(JSONObject params, @Context HttpServletRequest request) {

1.@Component:
使用@Component是讓該類能夠在其他地方被依賴使用,即可以直接在其他地方使用@Autowired來創建其實例對象。
可參考:https://blog.csdn.net/xyh820/article/details/7303330/

2.@Produces:
@Produces註釋用來指定將要返回給client端的數據標識類型(MIME)。@Produces可以作爲class註釋,也可以作爲方法註釋,方法的@Produces註釋將會覆蓋class的註釋。
a.返回給client字符串類型(text/plain)

@Produces(MediaType.TEXT_PLAIN)

b.返回給client爲json類型(application/json)

@Produces(MediaType.APPLICATION_JSON)

3.@Consumes:
@Consumes與@Produces相反,用來指定可以接受client發送過來的MIME類型,同樣可以用於class或者method,也可以指定多個MIME類型,一般用於@PUT,@POST
a.接受client參數爲字符串類型

@Consumes(MediaType.TEXT_PLAIN)

b.接受clent參數爲json類型

@Consumes(MediaType.APPLICATION_JSON)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章