com.opcooc.captcha.metadata.Response Maven / Gradle / Ivy
/*
* Copyright © 2020-2021 organization opcooc ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.opcooc.captcha.metadata;
import java.io.Serializable;
import java.text.MessageFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.opcooc.captcha.enums.RepCodeEnum;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
/**
* @author opcooc
*/
@Getter
@Setter
@ToString
public class Response implements Serializable {
private static final long serialVersionUID = -6190689122701100762L;
private int code;
private String message;
private Object data;
public Response(RepCodeEnum repCodeEnum) {
this.code = repCodeEnum.getCode();
this.message = repCodeEnum.getMsg();
}
public Response(RepCodeEnum repCodeEnum, Object data) {
this(repCodeEnum);
this.data = data;
}
public Response(int code, String message) {
this.code = code;
this.message = message;
}
@JsonIgnore
public boolean isOk() {
return this.code == RepCodeEnum.OK.getCode();
}
@JsonIgnore
public boolean isFail() {
return this.code != RepCodeEnum.OK.getCode();
}
//成功
public static Response ok() {
return new Response(RepCodeEnum.OK);
}
public static Response ok(String message) {
return new Response(RepCodeEnum.OK.getCode(), message);
}
public static Response ok(Object data) {
return new Response(RepCodeEnum.OK, data);
}
//失败
public static Response fail() {
return new Response(RepCodeEnum.ERROR);
}
public static Response fail(String message) {
return new Response(RepCodeEnum.FAIL.getCode(), message);
}
public static Response fail(RepCodeEnum repCodeEnum) {
return new Response(repCodeEnum);
}
public static Response fail(RepCodeEnum repCodeEnum, String message) {
return new Response(repCodeEnum.getCode(), message);
}
//错误
public static Response error() {
return new Response(RepCodeEnum.ERROR);
}
//格式化错误信息
public static Response parseError(RepCodeEnum repCodeEnum, Object... fieldNames) {
String msg = MessageFormat.format(repCodeEnum.getMsg(), fieldNames);
return new Response(repCodeEnum.getCode(), msg);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy