com.kasinf.framework.rest.config.SearchableConfiguration Maven / Gradle / Ivy
The newest version!
package com.kasinf.framework.rest.config;
import lombok.Getter;
import org.springframework.http.HttpStatus;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import java.net.URI;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
/**
* @author lkhsh
* 搜索方法配置属性
*/
@Getter
public class SearchableConfiguration {
private static final URI NO_URI = URI.create("");
private URI baseUri = NO_URI;
private URI basePath = NO_URI;
private int defaultPageSize = 20;
private int maxPageSize = 1000;
private int minPageSize = 10;
private String pageParamName = "page";
private String limitParamName = "size";
private String sortParamName = "sort";
private String searchParamName = "search";
private Boolean returnBodyOnCreate = Boolean.FALSE;
private Boolean returnBodyOnUpdate = Boolean.FALSE;
private Boolean fileFast = Boolean.TRUE;
private Map httpCode;
private final Map defaultCode = new HashMap() {{
put(HttpStatus.OK, 200);
put(HttpStatus.NOT_FOUND, 404);
put(HttpStatus.SERVICE_UNAVAILABLE, 500);
put(HttpStatus.METHOD_NOT_ALLOWED, 405);
put(HttpStatus.FORBIDDEN, 403);
put(HttpStatus.UNAUTHORIZED, 401);
put(HttpStatus.BAD_REQUEST, 400);
}};
public SearchableConfiguration setHttpCode(Map httpCode) {
if (httpCode != null) {
httpCode.forEach(defaultCode::put);
}
this.httpCode = Collections.unmodifiableMap(defaultCode);
return this;
}
public URI getBaseUri() {
return basePath != NO_URI ? basePath : baseUri;
}
/**
* 配置Hibernate Validator的快速失败模式
*
* @param fileFast 是否快速失败
* @return {@literal this}
*/
public SearchableConfiguration setFileFast(boolean fileFast) {
this.fileFast = fileFast;
return this;
}
/**
* 用于@SearchableControoler注解的统一请求前缀
*
* @param basePath 请求路径前缀
* @return {@literal this}
*/
public SearchableConfiguration setBasePath(String basePath) {
Assert.isTrue(!basePath.startsWith("http"), "Use a path not a URI");
basePath = StringUtils.trimTrailingCharacter(basePath, '/');
this.basePath = URI.create(basePath.startsWith("/") ? basePath : "/".concat(basePath));
Assert.isTrue(!this.basePath.isAbsolute(), "Absolute URIs are not supported as base path!");
return this;
}
/**
* 设值默认分页大小 {@link org.springframework.data.domain.Pageable}s.
*
* @param defaultPageSize 默认分页大小
* @return {@literal this}
*/
public SearchableConfiguration setDefaultPageSize(int defaultPageSize) {
Assert.isTrue(defaultPageSize > 0, "Page size must be greater than 0.");
this.defaultPageSize = defaultPageSize;
return this;
}
/**
* 设值分页最大条数
*
* @param maxPageSize 最大分页条数
* @return {@literal this}
*/
public SearchableConfiguration setMaxPageSize(int maxPageSize) {
Assert.isTrue(maxPageSize > defaultPageSize, "Maximum page size must be greater than defaultPageSize.");
this.maxPageSize = maxPageSize;
return this;
}
/**
* 设值最小分页条数
*
* @param minPageSize minimum page size.
* @return {@literal this}
*/
public SearchableConfiguration setMinPageSize(int minPageSize) {
Assert.isTrue(minPageSize <= defaultPageSize, "minimum page size must be small than defaultPageSize.");
this.minPageSize = minPageSize;
return this;
}
/**
* 设值请求中分页的参数名
*
* @param pageParamName 分页页数的参数名
* @return {@literal this}
*/
public SearchableConfiguration setPageParamName(String pageParamName) {
Assert.notNull(pageParamName, "Page param name cannot be null.");
this.pageParamName = pageParamName;
return this;
}
/**
* 设置请求中分页大小的参数名
*
* @param limitParamName 分页大小的参数名
* @return {@literal this}
*/
public SearchableConfiguration setLimitParamName(String limitParamName) {
Assert.notNull(limitParamName, "Limit param name cannot be null.");
this.limitParamName = limitParamName;
return this;
}
/**
* 设置请求中排序的参数名
*
* @param sortParamName 排序的参数名
* @return {@literal this}
*/
public SearchableConfiguration setSortParamName(String sortParamName) {
Assert.notNull(sortParamName, "Sort param name cannot be null.");
this.sortParamName = sortParamName;
return this;
}
/**
* 设置请求中搜索条件的参数名
*
* @param searchParamName 搜索条件参数名
* @return {@literal this}
*/
public SearchableConfiguration setSearchParamName(String searchParamName) {
Assert.notNull(searchParamName, "Search param name cannot be null.");
this.searchParamName = searchParamName;
return this;
}
/**
* 设置创建成功时,是否返回创建的bean信息
*
* @param returnBody 是否返回主体
* @return {@literal this}
*/
public SearchableConfiguration setReturnBodyOnCreate(Boolean returnBody) {
this.returnBodyOnCreate = returnBody;
return this;
}
/**
* 设置更新成功后,是否返回bean信息
*
* @param returnBodyOnUpdate 是否返回主体
* @return {@literal this}
*/
public SearchableConfiguration setReturnBodyOnUpdate(Boolean returnBodyOnUpdate) {
this.returnBodyOnUpdate = returnBodyOnUpdate;
return this;
}
}