Mustache 生成的 Restful API 中的 Java 函数参数

标签 java rest jax-rs mustache swagger

我有来自 swagger 的 WebService API mustache 文件:

{{>generatedAnnotation}}
{{#operations}}
public class {{classname}}ServiceImpl extends {{classname}}Service {

{{#operation}}
  @Override
  public Response {{nickname}}({{#allParams}}{{>serviceQueryParams}}{{>servicePathParams}}{{>serviceHeaderParams}}{{>serviceBodyParams}}{{>serviceFormParams}}{{#hasMore}},{{/hasMore}}{{/allParams}})
  throws NotFoundException {
      foo(...)
      return Response.ok().entity(new ApiResponseMessage(ApiResponseMessage.OK, "foo")).build();
  }
{{/operation}}

生成的是一个Restful API:

@Override
public Response findPets(List<String> tags,Integer limit) throws NotFoundException {
    foo(...)
    return Response.ok().entity(new ApiResponseMessage(ApiResponseMessage.OK, "foo")).build();
}

“serviceQueryParams”等是单独的 mustache 文件,如下所示:

{{#isQueryParam}}{{{dataType}}} {{paramName}}{{/isQueryParam}}

我想要的是调用一个函数 (foo),该函数采用与生成的 API 方法中相同的参数:

public Response findPets(List<String> tags,Integer limit) throws NotFoundException {
    foo(tags, limit);
    ...

另一个例子:

@Override
public Response addPet(NewPet pet) throws NotFoundException {
   foo(pet);
   ....

我已经在 java 中定义了 foo 函数,但我需要编辑 Mustache 文件以便正确生成代码。

最佳答案

看看api.mustache以文件为例,您只需更新您的 api.mustache 文件,如下所示:

foo({{#allParams}}{{#isFile}}fileDetail{{/isFile}}{{^isFile}}{{paramName}}{{/isFile}}{{#hasMore}},{{/hasMore}}{{/allParams}});

关于Mustache 生成的 Restful API 中的 Java 函数参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33477882/

相关文章:

java - 从单个构造函数参数推断泛型类类型参数

java - 我测试正确吗?

java - 在 ServletContextListener 中处理 Java 异常的首选方法

java - GSON : custom object deserialization

c++ - 无法打开包含文件 : 'cpprest/ws_client' : No such file or directory

spring-boot - Spring Boot使用ControllerAdvice中的自定义错误处理所有错误

java - Axis2-maven依赖失败

java - JAX-RS boolean 值与整数

java - 尝试访问使用 Jersey 开发的 POST Json 消费者端点时获取不受支持的媒体类型

java - 如何使用 Spring Boot Rest 的自定义异常?