gpt4 book ai didi

Java/Jackson - 'Unrecognized token' 传递 JSON 对象参数

转载 作者:行者123 更新时间:2023-12-02 10:39:14 26 4
gpt4 key购买 nike

使用 Jersey/Jackson 的 Java JAX-RS Web 服务,服务方法需要 JSON 形式的用户参数 (POJO)。客户端应用程序 (Angular 6) 发送包含 User 参数(序列化为 JSON)的 POST 请求。服务方法调用失败,并显示错误消息:“无法识别的 token ‘jsonUser’:正在等待(‘true’、‘false’或‘null’)”。

这是 User 类 (POJO) - 您可以看到我尝试使用 @JsonProperty 注释所有属性,但这是不必要的,因为我没有“重命名”它们:

import java.io.Serializable;

import javax.ws.rs.FormParam;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;

//import org.glassfish.jersey.media.multipart.FormDataParam;

/**
* JavaBean for passing the User properties between the UI app (Angular)
* and TearsWs. Implementation requires this to be serializable (JSON).
*/
@JsonIgnoreProperties({ "DELIM" })
public class User implements Serializable {
private String userName;
private String employeeId;
private String employeeName;
private String homeUnitCode;
private boolean certifier;
private HomeUnit[] tkHomeUnits;
private boolean supervisor;
private Employee[] whoISupervise;
private boolean hrStaff;
private boolean collector;

private final static String DELIM = ", ";

public User() {
}

// getters / setters
//@JsonProperty("userName")
public void setUserName(String ldapUid) {
this.userName = ldapUid;
}
public String getUserName() {
return this.userName;
}

//@JsonProperty("employeeId")
public void setEmployeeId(String employeeId) {
this.employeeId = employeeId;
}
public String getEmployeeId() {
return this.employeeId;
}

//@JsonProperty("employeeName")
public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}
public String getEmployeeName() {
return this.employeeName;
}

//@JsonProperty("homeUnitCode")
public void setHomeUnitCode(String homeUnitCode) {
this.homeUnitCode = homeUnitCode;
}
public String getHomeUnitCode() {
return this.homeUnitCode;
}

//@JsonProperty("certifier")
public void setCertifier(boolean certifier) {
this.certifier = certifier;
}
public boolean getCertifier() {
return this.certifier;
}

//@JsonProperty("tkHomeUnits")
public void setTkHomeUnits(HomeUnit[] tkHomeUnitCodes) {
this.tkHomeUnits = tkHomeUnitCodes;
}
public HomeUnit[] getTkHomeUnits() {
return this.tkHomeUnits;
}

//@JsonProperty("supervisor")
public void setSupervisor(boolean supervisor) {
this.supervisor = supervisor;
}
public boolean isSupervisor() {
return this.supervisor;
}

//@JsonProperty("whoISupervise")
public void setWhoISupervise(Employee[] whoISupervise) {
this.whoISupervise = whoISupervise;
}
public Employee[] getWhoISupervise() {
return this.whoISupervise;
}

//@JsonProperty("hrStaff")
public void setHrStaff(boolean hrStaff) {
this.hrStaff = hrStaff;
}
public boolean isHrStaff() {
return this.hrStaff;
}

//@JsonProperty("collector")
public void setCollector(boolean collector) {
this.collector = collector;
}
public boolean isCollector() {
return this.collector;
}

//methods
public boolean hasTauthority() {
return this.certifier || this.collector;
}

public String toString() {
int tkHUs = (tkHomeUnits == null) ? 0 : tkHomeUnits.length;
return "[User: "
+ "userName=" + this.userName + DELIM
+ "employeeId=" + this.employeeId + DELIM
+ "employeeName=" + this.employeeName + DELIM
+ "homeUnitCode=" + this.homeUnitCode + DELIM
+ "certifier=" + this.certifier + DELIM
+ "hrStaff=" + this.hrStaff + DELIM
+ "collector=" + this.collector + DELIM
+ "I can certify " + tkHUs + " homeUnits" + "]";
}
}

这是(Java)服务方法,它应该接受并处理 POST 请求:

/**
* Web service method.
*/
@POST
@Path("getTkHomeUnitEmployees")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response getTkHomeUnitEmployees(User user, @HeaderParam("X-Request-Param") String homeUnitCode) throws Exception {
String exceptionMessage;

if (user == null) {
exceptionMessage = "getTkHomeUnitEmployees() received a null User.";
log.error(exceptionMessage);
Response response = Response
.status(500)
.entity(exceptionMessage)
.build();
return response;
}
if (homeUnitCode == null || homeUnitCode.equals("")) {
exceptionMessage = "getTkHomeUnitEmployees() received a null HomeUnitCode.";
log.error(exceptionMessage);
Response response = Response
.status(500)
.entity(exceptionMessage)
.build();
return response;
}
if (!user.hasTauthority()) {
exceptionMessage = "getTkHomeUnitEmployees() received a request from a non-timekeeper and non-collector.";

log.error(exceptionMessage);
Response response = Response
.status(500)
.entity(exceptionMessage)
.build();
return response;
}
try {
Employee[] tkHomeUnitEmployees = new SecurityDao().getTkHomeUnitEmployees(user.getEmployeeId(), homeUnitCode);

Response response = Response
.ok(tkHomeUnitEmployees)
.header("Access-Control-Allow-Origin", "*")
.build();
return response;
} catch (Exception ex) {
exceptionMessage = "getTkHomeUnitEmployees(): " + ex;
Response response = Response
.status(500)
.entity(exceptionMessage)
.build();
return response;
}
}

将User对象(客户端,Javascript)转换为JSON并封装为HttpParams中的参数; POST 在请求正文中传递它。

这是(Angular)客户端方法,它将 POST 请求发送到 Web 服务:

getTkHomeUnitEmployees(user: User, homeUnitCode: string): Observable<Employee[]> {
const headers = new HttpHeaders()
.set('Content-Type', 'application/json')
.set('X-Request-Param', homeUnitCode); // homeUnitCode parameter in HttpHeaders

const httpOptions = {
headers: headers
};

let jsonUser: string = JSON.stringify(user);

const httpParams = new HttpParams()
.set('jsonUser', jsonUser);

let postUrl = this.wsUrl + 'getTkHomeUnitEmployees';
//postUrl += '?homeUnitCode=' + homeUnitCode; // homeUnitCode parameter as QueryParam

let obsArrayEmployees: Observable<Employee[]> = this.httpClient.post<Employee[]>(postUrl, httpParams, httpOptions);
return obsArrayEmployees;
}

...这里我正在调试客户端(@浏览器开发工具),并在 getTkHomeUnitEmployees() 方法中进行中断:

debug getTkHomeUnitEmployees

...我已在控制台中显示了 jsonUser 的值:

debug - jsonUser value

...这是响应中的错误:

error - Response 400

error - Response Unrecognized token

...这是请求参数。

error - Request payload

因此,Jackson JsonParser 似乎正在尝试读取并解析请求中发送的参数,但该参数在开头包含“jsonUser=”作为其值的一部分(要解析的 json)。这显然是错误的...

服务方法在实际输入/处理代码之前就崩溃了;我无法在服务方法中设置断点来检查参数的值。它的行为就像“参数无效,返回调用者”响应。

我想手动破解“jsonUser=”(@客户端),但它不在那里。在客户端,“jsonUser=”不是参数值的一部分;我相信这只是 http 参数的 key=value 语法(parameter-name=parameter-value),也许当参数封装到 HttpParams 对象中时它就被添加到了前面。

显然我做错了什么,但我一直无法弄清楚;我认为这是正确的方法,但显然不是。希望有人能尽快提供帮助,我已经被这个问题困扰了几天了。

最佳答案

您不需要将“用户”对象转换为字符串以传递到后端。尝试按原样传递用户对象。

this.httpClient.post<Employee[]>(postUrl, user, httpOptions);

还请检查传递的参数是否与公开的其余服务真正匹配。

关于Java/Jackson - 'Unrecognized token' 传递 JSON 对象参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53055153/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com