Skip to content

Spring Framework extension to enhance the @HttpExchange annotation for Rest Clients

Notifications You must be signed in to change notification settings

FBibonne/spring-httpexchange-extension

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Spring Framework extension to enhance @HttpExchange

@HttpExchange is an annotation from Spring Framework. It can be used to create Http interface to easily request a Rest API from a java spring application.

This lib makes easier the use of http interface avoiding boilerplate code

Getting started

Add the lib in your classpath

for the moment, copy-paste the main code in your since it is not published on maven.

Create your http interface with Exhange annotation

For example :

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.service.annotation.GetExchange;

import java.util.List;

public interface RegionContract {
    //same as @HttpExchange(method = "GET", value = "/nomenclature/{id}")
    @GetExchange("/nomenclature/{id}")
    List<Region> getRegions(@PathVariable String id);
}

BEST PRACTICE ! Instead of writing the interface, generate it from an OpenAPI spec in a contract first approach

Annotate it with @HttpInterface

For example :

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.service.annotation.GetExchange;
import fr.insee.demo.httpexchange.autobeangeneration.HttpInterface;

import java.util.List;

@HttpInterface(baseUrl = "https://base.url.of.the.api.I.consume/")
public interface RegionContract {
    //same as @HttpExchange(method = "GET", value = "/nomenclature/{id}")
    @GetExchange("/nomenclature/{id}")
    List<Region> getRegions(@PathVariable String id);
}

TIP You can also extend thie interface RegionContract to keep it pure from the framework

import fr.insee.demo.httpexchange.autobeangeneration.HttpInterface;

@HttpInterface(baseUrl = "https://base.url.of.the.api.I.consume/")
public interface RegionHttpInterface extends RegionContract{}

Enable the lib

Just annotate a class scanned by spring with @EnableHttpInterface :

import fr.insee.demo.httpexchange.autobeangeneration.EnableHttpInterface;

@EnableHttpInterface
// it can be a @Configuration class but it is not mandatory
class SimpleConfig{}

Enjoy the generated client bean in you services

@Component
public record GreatService(RegionContract regionContract){
    public List<Region> findRegionsById(RegionId regionId){
        // call the remote API, get the result, map it to Region and return the result
        return regionContract.getRegions(regionId.value());
    }
}

Other features

In @HttpInterface

In @EnableHttpInterface

  • you can use attribute basePackages to specify which packages (including subpackages) will be scanned to find @HttpInterface annotated interfaces. If basePackages is not specified, all classpath will be scanned.

About

Spring Framework extension to enhance the @HttpExchange annotation for Rest Clients

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages