Currently I have a project that uses the swagger-codegen-maven-plugin
to generate the swagger controllers with the delegatePattern
.
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>
[...]
Currently it generates the controller interfaces like this:
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();
}
}
But I like that the controller is generated with an HttpServletRequest
as parameters like this:
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);
}
}
Is it possible to make this happen?
Cheers
No, but you can inject the HttpServletRequest into your delegate/class controller like this:
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 is aware of the nature of the HttpServletRequest and automatically its scope is always set to request and it isn't a Singleton. So you will always have the current request at hand.