簡體   English   中英

傑克遜無法識別@JsonCreator

[英]@JsonCreator is not recognized using Jackson

我正在編寫一個簡單的rest API,它將在給定的主機名上安裝/啟動/停止代理。 作為API的一部分,我正在創建一個AgentInstruction的實例(下面的代碼)。 由於某種原因,應用程序將拋出JsonMappingException ,即:

Caused by: org.codehaus.jackson.map.JsonMappingException: No suitable constructor found for type [simple type, class com.sample.web.shared.rest.AgentInstruction]: can not instantiate from JSON object (need to add/enable type information?)

這似乎是一個奇怪的錯誤,因為@JsonCreator所有示例都@JsonCreator相同,也許我缺少某些配置,或者靜態字段是否有特殊之處?

package com.sample.web.shared.rest;

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

/**
 * Used for modifying agent setup and starting/stopping agents on specified
 * machines.
 * @since 1.0.0
 */
public class AgentInstruction {
    /**
     * Installation instruction value, used to tell the server to install
     * the agent (or reinstall) on a given machine.
     */
    public static final String INSTALL = "INSTALL";

    /**
     * Start instruction value, used to tell the server to start the agent
     * associated with the given machine.
     */
    public static final String START = "START";

    /**
     * Stop instruction value, used to tell the server to stop the agent
     * associated with the given machine.
     */
    public static final String STOP = "STOP";

    /**
     * Instruction which should be performed.
     */
    final private String instruction;

    /**
     * AgentInstruction which can create a specified installation instruction
     * @param instruction Instruction to give
     */
    @JsonCreator
    public AgentInstruction(@JsonProperty("instruction") String instruction) {
        this.instruction = instruction;
    }

    /**
     * Gets the instruction for the agent.
     * @return Instruction which dictates what action should be taken against
     * the agent
     */
    public String getInstruction() {
        return instruction;
    }

    @Override
    public String toString() {
           return "AgentInstruction: " + instruction;
    }

}

編輯:正在發送的JSON請求:

curl -H "ContentType: application/json" -X POST 
     -D '{"instruction":"INSTALL"}' 
     http://localhost:8080/operations-dashboard/machines/10.1.2.229/agent

Rest API正在處理請求:

/**
 * Installs and/or starts the agent on a particular machine depending on the instruction.
 * @param instruction AgentInstruction which should be given when updating the agent.
 * @param hostname Hostname where the agent should be installed.
 * @return NOT_FOUND if the machine cannot be found, OK if the agent is properly installed, or INTERNAL_SERVER_ERROR
 * if the agent installation has failed.
 * @since 1.0.0
 */
@POST
@Path("{hostname}/agent")
public Response updateAgent(AgentInstruction instruction, @PathParam("hostname") String hostname) {
    Machine machine = entityManager.find(Machine.class, hostname);
    if(machine == null) {
        return buildMachineNotFound(hostname);
    }

    // Find agent installation
    File agentInstallation = new File(installHome, "operations/deployments").listFiles(new FileFilter() {
        @Override
        public boolean accept(File pathname) {
            return (pathname.getName().startsWith("agent") && pathname.getName().endsWith("-rel.jar"));
        }
    })[0];

    switch(instruction.getInstruction()) {
        case AgentInstruction.INSTALL: return installAgent(machine, agentInstallation);
        case AgentInstruction.START: return startAgent(machine);
        case AgentInstruction.STOP: return stopAgent(machine);
        default: return Response.status(Response.Status.BAD_REQUEST).entity("Unknown instruction " + instruction).build();
    }
}

檢查您的課程的進口。 看起來您正在使用org.codehaus.jackson包(版本1.X)中的ObjectMapper,而注釋來自com.fasterxml.jackson.annotation (版本2.X)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM