Generate Server Implementation
Generate default implementation for server, you can use the base implementation to implement the server side.
-
Add Dependency
- Gradle
- Maven
dependencies {
annotationProcessor("io.github.danielliu1123:httpexchange-processor:latest")
}
compileJava {
options.compilerArgs.add("-AhttpExchangeConfig=${projectDir}/httpexchange-processor.properties")
}<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>io.github.danielliu1123</groupId>
<artifactId>httpexchange-processor</artifactId>
<version>latest</version>
</path>
</annotationProcessorPaths>
<compilerArgs>
<arg>-AhttpExchangeConfig=${project.basedir}/httpexchange-processor.properties</arg>
</compilerArgs>
</configuration>
</plugin> -
Generate Code
- Gradle
- Maven
./gradlew clean compileJava
./mvnw clean compile
The processor will generate a base implementation class for each interface annotated with @HttpExchange
or @RequestMapping
.
There are two types of generated code, ABSTRACT_CLASS
and INTERFACE
,
generated class name is the interface name with suffix Base
by default,
these can be configured in httpexchange-processor.properties
.
Example:
- ABSTRACT_CLASS
- INTERFACE
// source code
@HttpExchange("/user")
public interface UserApi {
@GetExchange("/{id}")
UserDTO getUser(@PathVariable("id") String id);
}
// generated code
public abstract class UserApiBase implements UserApi {
@Override
public UserDTO getUser(String id) {
throw new ResponseStatusException(HttpStatus.NOT_IMPLEMENTED);
}
}
// use generated code
@RestController
public class UserApiImpl extends UserApiBase {
@Override
public UserDTO getUser(String id) {
return new UserDTO(id, "Foo");
}
}
// source code
@HttpExchange("/user")
public interface UserApi {
@GetExchange("/{id}")
UserDTO getUser(@PathVariable("id") String id);
}
// generated code
@HttpExchange("/user")
public interface UserApiBase {
@GetExchange("/{id}")
default UserDTO getUser(@PathVariable("id") String id) {
throw new ResponseStatusException(HttpStatus.NOT_IMPLEMENTED);
}
}
// use generated code
@RestController
public class UserApiImpl implements UserApiBase {
@Override
public UserDTO getUser(String id) {
return new UserDTO(id, "Foo");
}
}
Custom Configuration
You can create a httpexchange-processor.properties
file in any directory and put configuration directives in it.
These apply to all source files in this directory and all child directories.
Starting from version 3.3.5.1, please use the httpExchangeConfig
option to specify the configuration file path,
it's friendly for incremental compilation.
Property | Description | Default Value |
---|---|---|
enabled | Enable the processor | true |
suffix | Generated base implementation class name suffix | Base (if suffix and prefix are both empty) |
prefix | Generated base implementation class name prefix | |
generatedType | Generated code type, support ABSTRACT_CLASS and INTERFACE | ABSTRACT_CLASS |
packages | Packages to scan, use comma to separate multiple packages, support Ant-style pattern | All packages |
outputSubpackage | Generated base implementation class output subpackage, relative to the package of the interface |
For example:
enabled=true
suffix=Base
prefix=
generatedType=INTERFACE
packages=com.example.api
outputSubpackage=generated
The httpexchange-processor.properties
file should be located in the project or module directory.
For Maven projects, it should be placed alongside the pom.xml
file.
For Gradle projects, it should be placed alongside the build.gradle
file.
When the version >= 3.3.5.1, please use the httpExchangeConfig
option to specify the configuration file path.
You can place the configuration file anywhere.