Skip to main content

Generate Server Implementation

Generate default implementation for server, you can use the base implementation to implement the server side.

  • Add Dependency

    dependencies {
    annotationProcessor("io.github.danielliu1123:httpexchange-processor:latest")
    }
    compileJava {
    options.compilerArgs.add("-AhttpExchangeConfig=${projectDir}/httpexchange-processor.properties")
    }
  • Generate Code

    ./gradlew clean compileJava

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:

// 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");
}
}

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.

tip

Starting from version 3.3.5.1, please use the httpExchangeConfig option to specify the configuration file path, it's friendly for incremental compilation.

PropertyDescriptionDefault Value
enabledEnable the processortrue
suffixGenerated base implementation class name suffixBase (if suffix and prefix are both empty)
prefixGenerated base implementation class name prefix
generatedTypeGenerated code type, support ABSTRACT_CLASS and INTERFACEABSTRACT_CLASS
packagesPackages to scan, use comma to separate multiple packages, support Ant-style patternAll packages
outputSubpackageGenerated base implementation class output subpackage, relative to the package of the interface

For example:

httpexchange-processor.properties
enabled=true
suffix=Base
prefix=
generatedType=INTERFACE
packages=com.example.api
outputSubpackage=generated
info

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.