使用 Jackson 无法识别@JsonCreator
@JsonCreator is not recognized using Jackson
我正在编写一个简单的 rest API,它将 install/start/stop 给定主机名上的代理。作为 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
的所有样本看起来都差不多,也许我缺少一些配置,或者静态字段有什么特别之处?
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)。
我正在编写一个简单的 rest API,它将 install/start/stop 给定主机名上的代理。作为 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
的所有样本看起来都差不多,也许我缺少一些配置,或者静态字段有什么特别之处?
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)。