Spring Boot with HttpServletRequest in method parameters 的 Swagger codegen 委托模式
Swagger codegen delegatePattern for Spring Boot with HttpServletRequest in method paremeters
目前我有一个项目使用 swagger-codegen-maven-plugin
生成带有 delegatePattern
的 swagger 控制器。
pom.xml
:
[...]
<plugin>
<groupId>io.swagger</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>2.3.1</version>
<executions>
<execution>
<id>generate-api-v1</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/src/main/resources/specs/v1.yaml</inputSpec>
<language>spring</language>
<apiPackage>test.foo.bar.v1</apiPackage>
<modelPackage>test.foo.bar.v1.v1.model</modelPackage>
<generateSupportingFiles>false</generateSupportingFiles>
<configOptions>
<java8>true</java8>
<dateLibrary>java8</dateLibrary>
<delegatePattern>true</delegatePattern>
<useOptional>true</useOptional>
<useBeanValidation>true</useBeanValidation>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
[...]
目前它生成这样的控制器接口:
public interface FooApi {
FooDelegate getDelegate();
@ApiOperation(value = "", nickname = "fooAction", notes = "", response = String.class)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Success", response = String.class)
})
@RequestMapping(value = "/fooAction",
produces = { "text/plain" },
method = RequestMethod.GET)
default ResponseEntity<String> fooAction() {
return getDelegate().fooAction();
}
}
但我喜欢控制器是用 HttpServletRequest
作为参数生成的,如下所示:
public interface FooApi {
FooDelegate getDelegate();
@ApiOperation(value = "", nickname = "fooAction", notes = "", response = String.class)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Success", response = String.class)
})
@RequestMapping(value = "/fooAction",
produces = { "text/plain" },
method = RequestMethod.GET)
default ResponseEntity<String> fooAction(HttpServletRequest request) {
return getDelegate().fooAction(request);
}
}
这有可能实现吗?
干杯
不,但是您可以像这样将 HttpServletRequest 注入到您的 delegate/class 控制器中:
public class FooApiController implements FooApi {
private final HttpServletRequest httpServletRequest;
@Autowired
public FooApiController(HttpServletRequest httpServletRequest) {
this.httpServletRequest = httpServletRequest;
}
@Override
public ResponseEntity<String> fooAction() {
// code here
}
}
Spring 了解 HttpServletRequest 的性质,并且自动将其范围始终设置为请求并且它不是单例。因此,您将始终拥有当前的请求。
对我有用。
另一种方法,我发现工作是将@Scope 与@Context 一起使用。
@Scope("request")
public class FooApiController implements FooApi {
@Context
private HttpServletRequest httpServletRequest;
...
Your code goes here
...
}
目前我有一个项目使用 swagger-codegen-maven-plugin
生成带有 delegatePattern
的 swagger 控制器。
pom.xml
:
[...]
<plugin>
<groupId>io.swagger</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>2.3.1</version>
<executions>
<execution>
<id>generate-api-v1</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/src/main/resources/specs/v1.yaml</inputSpec>
<language>spring</language>
<apiPackage>test.foo.bar.v1</apiPackage>
<modelPackage>test.foo.bar.v1.v1.model</modelPackage>
<generateSupportingFiles>false</generateSupportingFiles>
<configOptions>
<java8>true</java8>
<dateLibrary>java8</dateLibrary>
<delegatePattern>true</delegatePattern>
<useOptional>true</useOptional>
<useBeanValidation>true</useBeanValidation>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
[...]
目前它生成这样的控制器接口:
public interface FooApi {
FooDelegate getDelegate();
@ApiOperation(value = "", nickname = "fooAction", notes = "", response = String.class)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Success", response = String.class)
})
@RequestMapping(value = "/fooAction",
produces = { "text/plain" },
method = RequestMethod.GET)
default ResponseEntity<String> fooAction() {
return getDelegate().fooAction();
}
}
但我喜欢控制器是用 HttpServletRequest
作为参数生成的,如下所示:
public interface FooApi {
FooDelegate getDelegate();
@ApiOperation(value = "", nickname = "fooAction", notes = "", response = String.class)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Success", response = String.class)
})
@RequestMapping(value = "/fooAction",
produces = { "text/plain" },
method = RequestMethod.GET)
default ResponseEntity<String> fooAction(HttpServletRequest request) {
return getDelegate().fooAction(request);
}
}
这有可能实现吗?
干杯
不,但是您可以像这样将 HttpServletRequest 注入到您的 delegate/class 控制器中:
public class FooApiController implements FooApi {
private final HttpServletRequest httpServletRequest;
@Autowired
public FooApiController(HttpServletRequest httpServletRequest) {
this.httpServletRequest = httpServletRequest;
}
@Override
public ResponseEntity<String> fooAction() {
// code here
}
}
Spring 了解 HttpServletRequest 的性质,并且自动将其范围始终设置为请求并且它不是单例。因此,您将始终拥有当前的请求。
@Scope("request")
public class FooApiController implements FooApi {
@Context
private HttpServletRequest httpServletRequest;
...
Your code goes here
...
}