http-response-headers

This small java library is used with spring to use Annotations for HTTP Response Headers.

License

License

MIT
Categories

Categories

Net
GroupId

GroupId

net.dracoblue.spring
ArtifactId

ArtifactId

http-response-headers
Last Version

Last Version

0.2.0
Release Date

Release Date

Type

Type

jar
Description

Description

http-response-headers
This small java library is used with spring to use Annotations for HTTP Response Headers.
Project URL

Project URL

https://dracoblue.net
Source Code Management

Source Code Management

https://github.com/DracoBlue/http-response-headers.git

Download http-response-headers

How to add to project

<!-- https://jarcasting.com/artifacts/net.dracoblue.spring/http-response-headers/ -->
<dependency>
    <groupId>net.dracoblue.spring</groupId>
    <artifactId>http-response-headers</artifactId>
    <version>0.2.0</version>
</dependency>
// https://jarcasting.com/artifacts/net.dracoblue.spring/http-response-headers/
implementation 'net.dracoblue.spring:http-response-headers:0.2.0'
// https://jarcasting.com/artifacts/net.dracoblue.spring/http-response-headers/
implementation ("net.dracoblue.spring:http-response-headers:0.2.0")
'net.dracoblue.spring:http-response-headers:jar:0.2.0'
<dependency org="net.dracoblue.spring" name="http-response-headers" rev="0.2.0">
  <artifact name="http-response-headers" type="jar" />
</dependency>
@Grapes(
@Grab(group='net.dracoblue.spring', module='http-response-headers', version='0.2.0')
)
libraryDependencies += "net.dracoblue.spring" % "http-response-headers" % "0.2.0"
[net.dracoblue.spring/http-response-headers "0.2.0"]

Dependencies

compile (3)

Group / Artifact Type Version
org.springframework.boot : spring-boot-starter-web jar 1.3.2.RELEASE
org.springframework : spring-expression jar 4.2.4.RELEASE
javax.servlet : javax.servlet-api jar 3.1.0

test (1)

Group / Artifact Type Version
org.springframework.boot : spring-boot-starter-test jar 1.3.2.RELEASE

Project Modules

There are no modules declared in this project.

net.dracoblue.spring:http-response-headers

  • Latest Release: GitHub version
  • Build Status: Build Status

This small java library is used with spring to use Annotations for HTTP Response Headers.

Usage

You can specifiy @HttpResponseHeader and @HttpResponseHeaders for methods and classes. If an annotation is visible on method level it will ignore the class level one. If the class annotation provides the same header key like a method annotation, the method annotation will override the value of the class annotation.

Methods

/* One Header */
@HttpResponseHeader(name="X-Key", value="X-Value-Method")
public String handleExtraAnnotatedMethod() {
    return null;
}

/* Multiple headers */
@HttpResponseHeaders(
    {
        @HttpResponseHeader(name="X-Key-One", value="X-Value-One-Method"),
        @HttpResponseHeader(name="X-Key-Two", value="X-Value-Two-Method")
    }
)
public String handleMultipleExtraAnnotatedMethod() {
    return null;
}

Classes

@HttpResponseHeader(name="X-Key-For-Response-Header", value="X-Value-For-Response-Header-Class")
@HttpResponseHeaders(
    {
        @HttpResponseHeader(name="X-Key-For-Response-Headers-One", value="X-Value-For-Response-Headers-One-Class"),
        @HttpResponseHeader(name="X-Key-For-Response-Headers-Two", value="X-Value-For-Response-Headers-Two-Class")
    }
)
@Controller
public class FullyAnnotatedTestController  {
    /* ... */
}

Spring Expression Language

It's also possible, to use SpEL for the name and value property.

To enable parsing for the value property, enable it with valueExpression=true.

For example:

@HttpResponseHeader(name="Cache-Control", value="#{'max-age=' + (60*5)}", valueExpression=true)

will result in:

Cache-Control: max-age=300

If you use that to read properties (like java.version):

@HttpResponseHeader(name="X-Java-Version", value="#{environment.getProperty('java.version')}", valueExpression=true)

will result in:

X-Java-Version: 1.8.0_25

Installation

Replace VERSION with GitHub version or 0.3.0-SNAPSHOT.

Maven:

<dependency>
    <groupId>net.dracoblue.spring</groupId>
    <artifactId>http-response-headers</artifactId>
    <version>VERSION</version>
</dependency>

Gradle/Grails:

compile 'net.dracoblue.spring:http-response-headers:VERSION'

Add this to your SpringBootApplication:

@Autowired
HttpResponseHeaderHandlerInterceptor httpResponsHeaderHandlerInterceptor;

@Bean
public WebMvcConfigurer contentNegotiatorConfigurer()
{
    return new WebMvcConfigurerAdapter()
    {
       @Override
       public void addInterceptors(InterceptorRegistry registry)
       {
           registry.addInterceptor(httpResponsHeaderHandlerInterceptor);
       }                
    };
}

Other Projects / Interesting Links

License

This work is copyright by DracoBlue (http://dracoblue.net) and licensed under the terms of MIT License.

Versions

Version
0.2.0
0.1.0
0.0.2
0.0.1