Java 服务器端带注释的 REST 客户端库

Java server side annotated REST client library

我正在构建一个使用外部 REST 服务(从服务器端)的中间件服务。我目前正在使用 Spring 引导和 RestTemplate 进行远程调用。

    Map<String, String> urlVariables = new HashMap<>();
    urlVariables.put("address", IP);
    urlVariables.put("port", PORT);
    urlVariables.put("par1", parameter1);
    urlVariables.put("par2", parameter2);

    MyServiceResponse state =
            restTemplate.getForObject("http://{address}:{port}/service/{par1}/{par2}", MyServiceResponse.class, urlVariables);

我想知道是否有任何库提供注释以自动生成 REST 客户端,就像 Volley 在 Android 中所做的那样。

@GET(url="http://{address}:{port}/service/{par1}/{par2}")
public MyServiceResponse getCurrentState(String address, String port, String par1, String par2)

RESTEasy Proxy Framework:

Resteasy has a client proxy framework that allows you to use JAX-RS annotations to invoke on a remote HTTP resource. The way it works is that you write a Java interface and use JAX-RS annotations on methods and the interface.

您正在寻找这样的东西吗?:

https://github.com/dpalmisano/NoTube-Beancounter-2.0/blob/master/platform/src/main/java/io/beancounter/platform/rai/MyRaiTVService.java#L45

@POST
@Path("/login/auth")
public Response loginWithAuth(
        @FormParam("username") String username,
        @FormParam("token") String token

) {
    try {
        Validations.checkNotEmpty(username, "Missing username parameter");
        Validations.checkNotEmpty(token, "Missing MyRaiTV token parameter");
    } catch (Exception ex) {
        return error(ex.getMessage());
    }

也许这对你有帮助(来自code.openhub.net):

http://code.openhub.net/file?fid=tUDx4oQRk7m8Fbf7HmE_8y_W6YQ&cid=8pHdzAiENbA&s=Java%20server%20side%20annotated%20REST%20client%20library&pp=0&fl=Java&ff=1&filterChecked=true&fp=1329&mp,=1&ml=1&me=1&md=1&projSelected=true#L0

您可以为此使用 REST Gap。您只需要:

  • 具有Spring MVC 或 JAX-RS 风格的注释接口
  • 调用 REST Gap 工厂并传递您的接口和 RestTemplate 实例
  • 接收调用 REST 服务的接口实现

这是它在代码中的样子(对于 Spring-MVC 接口 IPetStoreService):

// Create client
IPetStoreService client = RESTTemplateSpringMVCFactory
    .create(restTemplate, "http://mypetstore.com/rest", IPetStoreService.class);

// Call it!
List<Pet> pets = client.listPets();

就是这样!