在哪里为swagger生成的服务器代码放置代码

时间:2018-01-10 16:18:22

标签: java swagger code-generation swagger-codegen

我刚开始用swagger来生成一个java Spring API。 这一代本身没有问题。我使用PetStore yaml生成SpringBoot服务器端代码而没有问题。

在我生成代码之后,我找不到一个好的教程/方式向我解释哪里最好放置我想写的自定义代码。将它直接写入生成的代码似乎不是一个好主意,因为如果我因为定义的变化而重新生成,你不​​想覆盖使用的代码。

你得到的存根像:

@Controller
public class PetApiController implements PetApi {
...
  public ResponseEntity<ModelApiResponse> uploadFile(@ApiParam(value = "ID of pet to update",required=true ) @PathVariable("petId") Long petId,
    @ApiParam(value = "Additional data to pass to server") @RequestPart(value="additionalMetadata", required=false)  String additionalMetadata,
      @ApiParam(value = "file detail") @RequestPart("file") MultipartFile file) {
      // do some magic!
      return new ResponseEntity<ModelApiResponse>(HttpStatus.OK);
  }
...
}

现在有办法填补“做一些魔术!”在生成期间/之后的部分,即对服务的调用,以便我可以将其用作一种入口点。例如自动装配一个具有相同方法但可以由我提供的服务,这样我就可以将我的实现与生成的代码分开。

@Controller
public class PetApiController implements PetApi {
...
@Autowired
PetApiService petApiService;
...
  public ResponseEntity<ModelApiResponse> uploadFile(@ApiParam(value = "ID of pet to update",required=true ) @PathVariable("petId") Long petId,
    @ApiParam(value = "Additional data to pass to server") @RequestPart(value="additionalMetadata", required=false)  String additionalMetadata,
      @ApiParam(value = "file detail") @RequestPart("file") MultipartFile file) {
      return petApiService.uploadFile(file);
  }
...
}

由于

1 个答案:

答案 0 :(得分:1)

没有简单的方法可以使用Swagger重新生成新代码,并阻止它覆盖您想要创建的任何代码。虽然如果你想改变api,有一种方法可以减轻所有的复制粘贴。而不是在“做一些魔术”评论上写下所有代码尝试在外部类中放入一个方法,所以在你需要用swagger重新生成代码的情况下,你只需要复制一行,I给你举个例子:

template <class BaseIterator>
struct DReferenceIterator
{
  using value_type = BaseIterator::value_type::element_type;
  using reference = value_type &;
  using pointer = value_type *;
  using difference_type = BaseIterator::difference_type
  using iterator_category = BaseIterator::iterator_category;

  explicit DReferenceIterator(const BaseIterator & other) : other(other) {}

  reference operator*() const { return **other; }
  pointer   operator->() const { return (*other).get(); }
  reference operator[](size_t n) const { return (*other)[n]; }

  DReferenceIterator& operator++() { ++other; return *this; }
  DReferenceIterator& operator--() { --other; return *this; }

  DReferenceIterator& operator+=(difference_type n) { other += n; return *this; }
  DReferenceIterator& operator-=(difference_type n) { other -= n; return *this; }
  difference_type operator-(DReferenceIterator& rhs) { return other - rhs.other; }
  bool operator<(DReferenceIterator& rhs) { return other < rhs.other; }

  // And all the other operators, in terms of those      

private: 
  BaseIterator other;
};

在我的外部课堂上,我称之为“GenerateResponse”:

<?php

namespace App\Http\Middleware;

use Closure;

class AddSubscription
{
    public function handle($request, Closure $next)
    {
        $response = $next($request);

        $data = $response->getData();
        $data['subscription'] = getSubscriptionData(); // Put your subscription data here
        $response->setData($data);

        return $response;
    }
}

所以你只需复制“return GenerateResponse.uploadFile(petId,additionalMetadata,file);”并且每次更改时只会创建一次自动装配的GenerateResponse。