All Projects → bingoohuang → spring-rest-client

bingoohuang / spring-rest-client

Licence: Apache-2.0 License
spring rest client

Programming Languages

java
68154 projects - #9 most used programming language
HTML
75241 projects

spring-rest-client

spring rest client

a rest api can be written using spring mvc like this:

@RestController
@RequestMapping("/another")
public class AnotherController {
    @RequestMapping("/add")
    public int add(@RequestParam("offset") int offset) {
        // do something
        return offset ;
    }
}

a rest client can be declared like this:

@RequestMapping("/another")
@SpringRestClientEnabled(baseUrl = "http://localhost:8080")
public interface AnotherApi {
    @RequestMapping("/add")
    int add(@RequestParam("offset") int offset);
}

And then import spring-rest-client config like this:

@Configuration
@ComponentScan
@SpringRestClientEnabledScan
public class SpringRestClientConfig {
}

And then you can call the api like this:

@Autowired AnotherApi anotherApi;

int a = 123;
int account = anotherApi.add(a);

Bypass spring boot request mapping

// for spring boot 2.0
import com.github.bingoohuang.springrestclient.annotations.SpringRestClientEnabled;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcRegistrations;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.lang.reflect.Method;

@Configuration
public class SpringFrontConfig {
    @Bean
    public WebMvcRegistrations webMvcRegistrationsAdapter() {
        return new WebMvcRegistrations() {
            @Override
            public RequestMappingHandlerMapping getRequestMappingHandlerMapping() {
                return new RequestMappingHandlerMapping() {
                    @Override protected void registerHandlerMethod(Object handler, Method method, RequestMappingInfo mapping) {
                        // by pass SpringRestClientEnabled interface
                        if (method.getDeclaringClass().isAnnotationPresent(SpringRestClientEnabled.class)) return;
                        super.registerHandlerMethod(handler, method, mapping);
                    }
                };
            }
        };
    }

}
// for spring boot 1.x

import com.github.bingoohuang.springrestclient.annotations.SpringRestClientEnabled;
import org.springframework.boot.autoconfigure.web.WebMvcRegistrationsAdapter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.lang.reflect.Method;

@Configuration
public class SpringConfig {
    @Bean
    public WebMvcRegistrationsAdapter webMvcRegistrationsAdapter() {
        return new WebMvcRegistrationsAdapter() {
            @Override
            public RequestMappingHandlerMapping getRequestMappingHandlerMapping() {
                return new RequestMappingHandlerMapping() {
                    @Override
                    protected void registerHandlerMethod(Object handler, Method method, RequestMappingInfo mapping) {
                        // by pass SpringRestClientEnabled interface
                        if (method.getDeclaringClass().isAnnotationPresent(SpringRestClientEnabled.class)) return;
                        super.registerHandlerMethod(handler, method, mapping);
                    }
                };
            }
        };
    }
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].