Java / Jackson-“无法识别的令牌”传递JSON对象参数

时间:2018-10-29 23:03:43

标签: java json post deserialization

带有Jersey / Jackson的Java JAX-RS Web服务,一种服务方法期望用户参数(POJO)为JSON。客户端应用程序(角度6)发送包含User参数(序列化为JSON)的POST请求。服务方法调用失败,并显示错误消息:“无法识别的令牌'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)。这显然是错误的...

服务方法在实际输入/处理代码之前就炸毁了;我无法在service方法内设置断点来检查参数的值。它表现为“参数无效,返回到调用方”响应。

我想手动从其中(“ @客户端”)入侵“ jsonUser =“,但它不存在。在客户端,“ jsonUser =”不是参数值的一部分;我相信这只是http参数的key = value语法(参数名称=参数值),也许在将参数封装到HttpParams对象中时使用它作为前缀。

很明显,我做错了事,但我一直无法弄清楚。我认为这是执行此操作的正确方法,但显然不是。希望有人能尽快提供帮助,我已经坚持了几天。

1 个答案:

答案 0 :(得分:1)

您不需要隐蔽'user'对象到字符串即可传递给后端。尝试按原样传递用户对象。

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

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