btz
btz

Reputation: 75

@JsonCreator is not recognized using Jackson

I am writing a simple rest API which will install/start/stop an agent on a given hostname. As part of the API I am creating an instance of an AgentInstruction (code below). For some reason, the application is throwing a JsonMappingException, which is:

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?)

This seems like an odd error, since all samples of @JsonCreator look more or less the same, maybe I am missing some configuration, or there is something special about the static fields?

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;
    }

}

EDIT: JSON request which is being sent:

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

Rest API which is handling request:

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

Upvotes: 0

Views: 2726

Answers (1)

Alexey Gavrilov
Alexey Gavrilov

Reputation: 10853

Check the imports of your classes. It looks like you are using the ObjectMapper from the org.codehaus.jackson package (version 1.X) while the annotations are from com.fasterxml.jackson.annotation (version 2.X).

Upvotes: 2

Related Questions