在Spring MVC控制器中,我可以使用@PathVariable获取路径变量,以获取@RequestMapping中定义的变量的值.如何在拦截器中获取变量的值?
非常感谢你!
我想为我的$ http服务添加一个响应拦截器,以便进行错误处理.拦截器逻辑包括在必要的情况下使用$ http向服务器发送错误消息,但我不希望向服务器发送有关错误消息的错误消息,我的意思是,我希望在向服务器发送错误消息时禁用我的拦截器.
我的想法是创建一个名为'remote_log'的服务,并将所有向服务器发送错误所需的代码放入其中.该服务当然将使用$ http服务并将其置于其依赖列表中.
然后将拦截器的依赖关系添加到'remote_log'服务,并在需要向服务器发送错误时使用拦截器内的'remote_log'.问题是:
当$ http服务仍未实例化/可访问时,必须使用$ httpProvider定义拦截器,因此,拦截器代码内部不能依赖于$ http服务,因为发生了"循环依赖"错误.
我认为我唯一的选择是在我的'remote_log'中创建一个单独的$ http服务实例,这个实例不使用我在创建拦截器时设置的$ httpProvider配置.我的问题是:我怎么能这样做?还有其他想法吗?
我刚刚注意到新Interceptor中不再支持可能在先前的HTTP RequestsOption中使用的Header对象.
这是新的拦截器逻辑:
// Get the auth header from the service.
const authHeader = this.auth.getAuthorizationHeader();
// Clone the request to add the new header.
const authReq = req.clone({headers: req.headers.set('Authorization', authHeader)});
Run Code Online (Sandbox Code Playgroud)
我现在有两种方法可以在此请求中添加标题:
例:
headers?: HttpHeaders;
headers: req.headers.set('token1', 'asd')
Run Code Online (Sandbox Code Playgroud)
setHeaders?: {
[name: string]: string | string[];
};
setHeaders: {
'token1': 'asd',
'token2': 'lol'
}
Run Code Online (Sandbox Code Playgroud)
如何在此请求中有条件地添加多个标头?与我以前使用Header对象相同:
myLovellyHeaders(headers: Headers) {
headers.set('token1', 'asd');
headers.set('token2', 'lol');
if (localStorage.getItem('token1')) {
headers.set('token3', 'gosh');
}
}
const headers = new Headers();
this.myLovellyHeaders(headers);
Run Code Online (Sandbox Code Playgroud) 我试图拦截401和403错误刷新用户令牌,但我不能让它运作良好.我所取得的就是这个拦截器:
app.config(function ($httpProvider) {
$httpProvider.interceptors.push(function ($q, $injector) {
return {
// On request success
request: function (config) {
var deferred = $q.defer();
if ((config.url.indexOf('API URL') !== -1)) {
// If any API resource call, get the token firstly
$injector.get('AuthenticationFactory').getToken().then(function (token) {
config.headers.Authorization = token;
deferred.resolve(config);
});
} else {
deferred.resolve(config);
}
return deferred.promise;
},
response: function (response) {
// Return the promise response.
return response || $q.when(response);
},
responseError: function (response) {
// Access token invalid …Run Code Online (Sandbox Code Playgroud) 我正在使用Angular 4.3.1和HttpClient.有一个HttpInterceptor来设置一些标题.
在一些http get请求中,我需要设置一个不同的头.无论如何我可以为这个特定的HttpRequest传递一些param给这个HttpInterceptor?
@Injectable()
export class MyHttpInterceptor implements HttpInterceptor {
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
if(request.custom.param1) // how can i do this
request = request.clone({
setHeaders: {
'header1': 'xxxxxx'
}
});
else
request = request.clone({
setHeaders: {
'header2': 'yyyyyy'
}
});
return next.handle(request);
}
}
Run Code Online (Sandbox Code Playgroud) 我想创建一个异常处理程序,它将拦截我项目中的所有控制器.这可能吗?看起来我必须在每个控制器中放置一个处理程序方法.谢谢你的帮助.我有一个发送Json响应的弹簧控制器.因此,如果发生异常,我想发送一个可以从一个地方控制的错误响应.
我在vuex商店成功登录之后存储令牌,如下所示:
axios.post('/api/auth/doLogin.php', params, axiosConfig)
.then(res => {
console.log(res.data); // token
this.$store.commit('login', res.data);
})
Run Code Online (Sandbox Code Playgroud)
axiosConfig是我只设置baseURL的文件,export default { baseURL: 'http://localhost/obiezaca/v2' }params只是发送到后端的数据.
我的vuex文件看起来是:
import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex);
export const store = new Vuex.Store({
state: {
logged: false,
token: ''
},
mutations: {
login: (state, response) => {
state.logged = true;
state.token = response;
console.log('state updated');
console.log('state.logged flag is: '+state.logged);
console.log('state.token: '+state.token);
},
logout: (state) => {
state.logged = false;
state.token = '';
}
}
});
Run Code Online (Sandbox Code Playgroud)
它工作正常,我可以根据 …
有许多adhoc库支持angular2的上传/下载进度,我不知道如何使用native angular2 http api在上传/下载时显示进度.
我想使用原生http api的原因是因为我想利用它
有一篇关于如何使用angular的http api进行上传/下载的好文章
但该文章提到,没有本土方式来支持进步.
有没有人尝试使用http api来显示进度?
如果没有,你知道角度回购中的问题吗?
我正在使用改造.要捕获响应,我正在使用Interceptor:
OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.interceptors().add(myinterceptor);
Run Code Online (Sandbox Code Playgroud)
这是拦截器的代码:
new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Response response = chain.proceed(request);
if (path.equals("/user")){
String stringJson = response.body().string();
JSONObject jsonObject = new JSONObject(stringJson);
jsonObject.put("key",1);
//here I need to set this new json to response and then return this response
Run Code Online (Sandbox Code Playgroud)
如何在OkHttp响应中更改正文?
我Interceptor在我的 Android 应用程序中使用自定义和 Retrofit 客户端,在某些特定情况下会引发异常。我正在尝试使用 Kotlin 协程使其工作。
问题是我无法处理前面提到的错误,因为在从 Interceptor 实例中抛出异常的那一刻,它会使整个应用程序崩溃,而不是在协程的try/catch语句中被捕获。当我使用 Rx 实现时,异常完美地传播到onError回调中,在那里我能够以我需要的方式处理它。
我想这与用于网络调用的底层线程有某种关系,请从调用的地方、在抛出异常之前的拦截器和堆栈跟踪中查看下面的日志:
2019-11-04 17:17:34.515 29549-29729/com.app W/TAG: Running thread: DefaultDispatcher-worker-1
2019-11-04 17:17:45.911 29549-29834/com.app W/TAG: Interceptor thread: OkHttp https://some.endpoint.com/...
2019-11-04 17:17:45.917 29549-29834/com.app E/AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher
Process: com.app, PID: 29549
com.app.IllegalStateException: Passed refresh token can\'t be used for refreshing the token.
at com.app.net.AuthInterceptor.intercept(AuthInterceptor.kt:33)
Run Code Online (Sandbox Code Playgroud)
我应该怎么做才能正确地从拦截器中捕获和处理这个异常?我错过了什么吗?
interceptor ×10
angular ×3
android ×2
angularjs ×2
http ×2
okhttp ×2
spring ×2
axios ×1
exception ×1
header ×1
java ×1
javascript ×1
retrofit ×1
service ×1
spring-mvc ×1
typescript ×1
vue.js ×1
vuejs2 ×1
vuex ×1