使用杰克逊不承认@JsonCreator

时间:2015-05-01 19:31:07

标签: java json rest jackson

我正在编写一个简单的rest API,它将在给定的主机名上安装/启动/停止代理。作为API的一部分,我正在创建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

这似乎是一个奇怪的错误,因为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; } } 的所有样本看起来或多或少都相同,也许我缺少一些配置,或者静态字段有什么特别之处?

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

编辑:正在发送的JSON请求:

/**
 * 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();
    }
}

正在处理请求的Rest API:

CREATE TABLE TEST (ID INTEGER);
CREATE TABLE "Test" (ID INTEGER); -- Gives Errors , table already exists

0 个答案:

没有答案