Skip to content
This repository has been archived by the owner on Mar 4, 2022. It is now read-only.

Commit

Permalink
common comment
Browse files Browse the repository at this point in the history
  • Loading branch information
itning committed May 7, 2019
1 parent da09bfb commit ceccdb4
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,86 @@
* @author itning
*/
public class RestModel<T> implements Serializable {
/**
* 状态码
*/
private int code;
/**
* 消息
*/
private String msg;
/**
* 负载
*/
private T data;

/**
* 将{@link Mono<T>}转换为{@link Mono<RestModel>}
* 使用默认状态码和消息
*
* @param data 负载数据
* @param <T> 数据类型
* @return {@link RestModel}
*/
public static <T> Mono<RestModel> toMono(Mono<T> data) {
return data.map(RestModel::new);
}

/**
* 将{@link Mono<T>}转换为{@link Mono<RestModel>}
*
* @param status 状态码
* @param msg 消息
* @param data 数据
* @param <T> 数据类型
* @return {@link RestModel}
*/
public static <T> Mono<RestModel> toMono(HttpStatus status, String msg, Mono<T> data) {
return data.map(r -> new RestModel<>(status, msg, r));
}

/**
* 将{@link Mono<T>}转换为{@link Mono<ServerResponse>}
* 并返回200状态码且使用默认消息
*
* @param data 数据
* @param <T> 数据类型
* @return {@link ServerResponse}
*/
public static <T> Mono<ServerResponse> ok(Mono<T> data) {
return ServerResponse.ok().body(toMono(data), RestModel.class);
}

/**
* 将{@link Mono<T>}转换为{@link Mono<ServerResponse>}
* 并返回201状态码且使用默认消息
*
* @param data 数据
* @param <T> 数据类型
* @return {@link ServerResponse}
*/
public static <T> Mono<ServerResponse> created(Mono<T> data) {
return created(data, "创建成功");
}

/**
* 将{@link Mono<T>}转换为{@link Mono<ServerResponse>}
* 并返回201状态码
*
* @param data 数据
* @param msg 消息
* @param <T> 数据类型
* @return {@link ServerResponse}
*/
public static <T> Mono<ServerResponse> created(Mono<T> data, String msg) {
return created(msg, data);
}

public static <T> Mono<ServerResponse> created(String msg, Mono<T> data) {
return ServerResponse.status(HttpStatus.CREATED).body(toMono(HttpStatus.CREATED, msg, data), RestModel.class);
}

/**
* 返回204状态码不携带任何数据消息
*
* @return {@link ServerResponse}
*/
public static Mono<ServerResponse> noContent() {
return ServerResponse.noContent().build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,24 @@ public static void mustTeacherLogin(ServerRequest request) {
}
}

/**
* 获取身份ID
*
* @param request {@link ServerRequest}
* @return 身份ID
* @throws PermissionsException 如果获取失败
*/
public static String getNo(ServerRequest request) {
return request.queryParam("no").orElseThrow(() -> new PermissionsException("no is null"));
}

/**
* 获取姓名
*
* @param request {@link ServerRequest}
* @return 姓名
* @throws PermissionsException 如果获取失败
*/
public static String getName(ServerRequest request) {
return request.queryParam("name").orElseThrow(() -> new PermissionsException("name is null"));
}
Expand Down

0 comments on commit ceccdb4

Please sign in to comment.