詳解SpringBoot多跨域請求的支持(JSONP)
在我們做項(xiàng)目的過程中,有可能會遇到跨域請求,所以需要我們自己組裝支持跨域請求的JSONP數(shù)據(jù),而在4.1版本以后的SpringMVC中,為我們提供了一個AbstractJsonpResponseBodyAdvice的類用來支持jsonp的數(shù)據(jù)(SpringBoot接收解析web請求是依賴于SpringMVC實(shí)現(xiàn)的)。下面我們就看一下怎么用AbstractJsonpResponseBodyAdvice來支持跨域請求。
使用AbstractJsonpResponseBodyAdvice來支持跨域請求很簡單,只需要繼承這個類就可以了。具體代碼如下:
package com.zkn.learnspringboot.config;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.AbstractJsonpResponseBodyAdvice;
/**
* Created by wb-zhangkenan on 2016/12/1.
*/
@ControllerAdvice(basePackages = "com.zkn.learnspringboot.web.controller")
public class JsonpAdvice extends AbstractJsonpResponseBodyAdvice{
public JsonpAdvice() {
super("callback","jsonp");
}
}
下面我們寫個類來測試一下:
package com.zkn.learnspringboot.web.controller;
import com.zkn.learnspringboot.domain.PersonDomain;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by wb-zhangkenan on 2016/12/1.
*/
@RestController
@RequestMapping("/jsonp")
public class JsonpTestController {
@Autowired
private PersonDomain personDomain;
@RequestMapping(value = "/testJsonp",produces = MediaType.APPLICATION_JSON_VALUE)
public PersonDomain testJsonp(){
return personDomain;
}
}
當(dāng)我們發(fā)送請求為:http://localhost:8003/jsonp/testJsonp的時候,結(jié)果如下:

當(dāng)我們發(fā)送的請求為:http://localhost:8003/jsonp/testJsonp?callback=callback的時候,結(jié)果如下所示:

看到區(qū)別了嗎?當(dāng)我們在請求參數(shù)中添加callback參數(shù)的時候,返回的數(shù)據(jù)就是jsonp的,當(dāng)我們請求參數(shù)中不帶callback的時候,返回的數(shù)據(jù)是json的??梢宰屛覀兎奖愕撵`活運(yùn)用。下面再奉上一個jsonp的完整案例。
前臺頁面:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<script src="resources/js/jquery-2.1.4.min.js" type="text/javascript"></script>
</head>
<body>
<input type="button" value="測試jsonp請求" onclick="testJsonp()" />
<script type="text/javascript">
function testJsonp() {
$.ajax({
type:'get',
url:'http://localhost:8003/jsonp/testJsonp',
dataType:'jsonp',
jsonp:"callback",
success:function (data) {
alert(data.userName+" "+data.passWord);
},
error:function (err) {
alert('出現(xiàn)錯誤了!!!');
}
});
}
</script>
</body>
</html>
后臺代碼1:
package com.zkn.learnspringmvc.news.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* Created by zkn on 2016/12/3.
*/
@Controller
public class JsonpTestController {
@RequestMapping("testJsonp")
public String testJsonp(){
return "jsonp";
}
}
下面我們發(fā)送請求如下:http://localhost:8080/LearnSpringMvc/testJsonp

當(dāng)我們點(diǎn)擊測試jsopn請求這個按鈕的時候,效果如下:

我們成功的實(shí)現(xiàn)了一個跨越的請求。更詳細(xì)的請求信息如下:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Boot中自定義注解結(jié)合AOP實(shí)現(xiàn)主備庫切換問題
這篇文章主要介紹了Spring Boot中自定義注解+AOP實(shí)現(xiàn)主備庫切換的相關(guān)知識,本篇文章的場景是做調(diào)度中心和監(jiān)控中心時的需求,后端使用TDDL實(shí)現(xiàn)分表分庫,需要的朋友可以參考下2019-08-08
Java8深入學(xué)習(xí)系列(一)lambda表達(dá)式介紹
Java8最值得學(xué)習(xí)的特性就是Lambda表達(dá)式和Stream API,所以我們學(xué)習(xí)java8的第一課就是學(xué)習(xí)lambda表達(dá)式,下面這篇文章主要給大家介紹了關(guān)于Java8學(xué)習(xí)之lambda的相關(guān)資料,文中介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。2017-08-08
Springboot+WebSocket+Netty實(shí)現(xiàn)在線聊天/群聊系統(tǒng)
這篇文章主要實(shí)現(xiàn)在好友添加、建群、聊天對話、群聊功能,使用Java作為后端語言進(jìn)行支持,界面友好,開發(fā)簡單,文章中有詳細(xì)的代碼示例供大家參考,需要的朋友可以參考下2023-08-08
Resilience4J通過yml設(shè)置circuitBreaker的方法
Resilience4j是一個輕量級、易于使用的容錯庫,其靈感來自Netflix Hystrix,但專為Java 8和函數(shù)式編程設(shè)計,這篇文章主要介紹了Resilience4J通過yml設(shè)置circuitBreaker的方法,需要的朋友可以參考下2022-10-10
SpringFox實(shí)現(xiàn)自動生成RESTful?API文檔
在開發(fā)?RESTful?API?時,編寫?API?文檔是一個重要的任務(wù),這篇文章為大家介紹了如何使用?SpringFox?自動生成?RESTful?API?文檔,并提供示例代碼,需要的可以參考一下2023-06-06
詳解Java?ReentrantReadWriteLock讀寫鎖的原理與實(shí)現(xiàn)
ReentrantReadWriteLock讀寫鎖是使用AQS的集大成者,用了獨(dú)占模式和共享模式。本文和大家一起理解下ReentrantReadWriteLock讀寫鎖的實(shí)現(xiàn)原理,需要的可以了解一下2022-10-10

