Microprofile JAX-RS格式错误的响应

问题描述

目前,我正在Gradle中为Eclipse Microprofile(v 3.3)和Payara Micro(v 5.2020.4)创建REST API的一个小项目,但是最近开始以一种奇怪的格式发送响应。

以下是恢复密码的示例响应:

预期的响应:

{
    "code": 1000,"errors": null,"message": "New password sent","requestId": "a37c1e44-4732-43df-a43a-d9b6873a67e4","status": "Success","title": "resource.UserResource","data": {
        "userEmail": "user@domain","newPassword": "g7sEPafyXE5l5mtB","smtpResponse": "250 2.0.0 Ok: queued on sas8-6bf5c5d991b2 as 1602093082-8uXplwmsxs-pKmSJT1c\n"
    }
}

格式错误(当前响应):

{
    "type": "objectResponse","code": 1000,"requestId": "01c25fbe-709f-4b53-991b-7f15b4b2e7d6","data": "{\"userEmail\":\"user@domain\",\"newPassword\":\"dHsh5bCilJGtYZDQ\",\"smtpResponse\":\"250 2.0.0 Ok: queued on iva3-dd2bb2ff2b5f as 1602092387-TfEDJK66bY-djmC72ZO\\n\"}"
}

有时项目可以正常运行并按预期工作,但是有时它会以不良格式发送响应,我已经尝试降级Payara版本(v 5.194),Microprofile版本(v 3.2、3.1,和3.0),但问题仍然存在。

这是我的响应对象:

基本回复

@Data
@NoArgsConstructor
@ToString
@JsonbNillable
public class BaseResponse implements Serializable {

    private static final long serialVersionUID = 1L;
    protected int code;
    protected UUID requestId;
    protected ResponseStatus status;
    protected String title;
    protected String message;
    protected ResponseError errors;

    public enum ResponseStatus {
        Success,Failed,In_Progress;
    }
}

对象响应

@Data
@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
@ToString
@JsonbNillable
@Dependent
public class ObjectResponse<T> extends BaseResponse {

    private static final long serialVersionUID = 1L;
    private T data;
}

要在ObjectResponse中填充数据属性,我使用一个JsonObjectBuilder,如该示例

JsonObjectBuilder json = Json.createObjectBuilder();
json.add("userEmail",email);
json.add("newPassword",randomPassword);
json.add("smtpResponse",transport.getLastServerResponse());

ObjectResponse<JsonObject> data = new ObjectResponse<>();
data.setCode(1000);
data.setRequestId(requestId);
data.setTitle(AuthResource.class.getCanonicalName());
data.setMessage("New password sent");
data.setStatus(ResponseStatus.Success);
data.setData(json.build());
data.setErrors(null);

Response.ResponseBuilder builder = Response.status(200).entity(data);
builder.header("Request-ID",requestId.toString());
asyncResponse.resume(builder.build());

谢谢,如果您能指导我,我将不胜感激。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)