How to Add Custom header in spring boot and java
**Spring Boot (Java):**
In Spring Boot, you can add custom headers to HTTP responses using filters or interceptors. Here's an example of how you can do it using an interceptor:
1. Create a class that implements `HandlerInterceptor` interface:
```java
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
@Component
public class CustomHeaderInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
response.addHeader("Custom-Header", "Header-Value");
return true;
}
}
```
2. Register the interceptor in your Spring Boot application:
```java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Autowired
private CustomHeaderInterceptor customHeaderInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(customHeaderInterceptor);
}
}
```
Now, every HTTP response from your Spring Boot application will include the custom header `"Custom-Header"` with the value `"Header-Value"`.
**Angular (TypeScript):**
In Angular, you can add custom headers to HTTP requests using an `HttpInterceptor`. Here's an example of how to create one:
1. Create an interceptor:
```typescript
import { Injectable } from '@angular/core';
import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent } from '@angular/common/http';
import { Observable } from 'rxjs';
@Injectable()
export class CustomHttpInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const modifiedReq = req.clone({
setHeaders: {
'Custom-Header': 'Header-Value'
}
});
return next.handle(modifiedReq);
}
}
```
2. Provide the interceptor in your Angular module:
```typescript
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
import { AppComponent } from './app.component';
import { CustomHttpInterceptor } from './custom-http-interceptor';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
HttpClientModule
],
providers: [
{ provide: HTTP_INTERCEPTORS, useClass: CustomHttpInterceptor, multi: true }
],
bootstrap: [AppComponent]
})
export class AppModule { }
```
Now, every HTTP request from your Angular application will include the custom header `"Custom-Header"` with the value `"Header-Value"`.
These examples demonstrate how to add custom headers in Spring Boot (Java) and Angular (TypeScript).
Comments
Post a Comment