edrb2409
8/18/2016 - 8:05 PM

Using @BeanParam

Using @BeanParam

@BeanParam

When having several @*Param in a resource, you can group them into a POJO

From:

@GET
@Path("{moduleName}")
public Response getModule(
        @PathParam("moduleName") final String moduleName,
        @NotEmpty(message = "databaseName can not be null or empty") @QueryParam("databaseName") final String databaseName,
        @NotEmpty(message = "databasePort can not be null or empty") @QueryParam("databasePort") final String databasePort,
        @IPAddress @QueryParam("ipAddress") final String ipAddress) {
        
    ...

}

Refactor to:

ImportParams.java


public class ImporterParam {

    @PathParam("moduleName")
    private String moduleId;

    private final Repository repoInformation;

    public ImporterParam(
            @NotEmpty(message = "databaseName can not be null or empty") @QueryParam("databaseName") final String databaseName,
            @NotEmpty(message = "databasePort can not be null or empty") @QueryParam("databasePort") final String databasePort,
            @IPAddress @QueryParam("ipAddress") final String ipAddress {
        repoInformation = new DesignerRepositoryInformationBuilder(algnPath).withIPAddress(ipAddress)
                .withDatabasePort(databasePort).withDatabaseName(databaseName).build();
    }

    public String getModuleId() {
        return moduleId;
    }

    public Repository getRepoInformation() {
        return repoInformation;
    }

    @Override
    public String toString() {
        return "ImporterParam: {moduleId: " + moduleId + ", repoInformation: " + repoInformation + "}";
    }
}

Refactored service:

@GET
@Path("{moduleName}")
public Response getModules(@BeanParam ImporterParam importerParams) {
    
    ...
    
}