改造2和Spring RestController

pio*_*rpo 6 java spring-mvc retrofit2

我想创建一个包含3个模块的项目:

  • 移动应用程序(改型?)
  • Spring端点(@RestControler)
  • Interface (contains communication entities and rest methods definition)

Data classes are easy part as I can use this same parser on both ends, or just don't use any gson / jackson / *son annotations at all.

The problem is:

How to define Retrofit interface and Spring in a way that do not require manual synchronization of both files?

Retrofit:

public interface GitHubService {
  @GET("users/{user}/repos")
  Call<List<Repo>> listRepos(@Path("user") String user);
}
Run Code Online (Sandbox Code Playgroud)

Spring:

public class GitHubService {
  @RequestMapping(value="users/{user}/repos", mehod=GET)
  List<Repo> listRepos(@PathVariable("user") String user);
}
Run Code Online (Sandbox Code Playgroud)

小智 0

从外观上看,Retrofit 不支持这一点。但RestGap确实:

您要求的是您可以在 Spring MVC 中定义您的服务器接口 - 这样,您可以创建一个接口 IGithubService,其中所有方法都以通常的方式注释@RequestMapping等。您可以:

  • 为客户端使用该接口
  • 在服务器上实现该接口。

RestGap 支持使用 Spring MVC 注释(您的情况)和 JAX RS 注释声明的接口。