gpt4 book ai didi

java - 使用 Jackson 无法识别 @JsonCreator

转载 作者:行者123 更新时间:2023-12-01 11:35:56 25 4
gpt4 key购买 nike

我正在编写一个简单的 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 的所有示例看起来或多或少相同,也许我缺少一些配置,或者静态字段有什么特殊之处?

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

关于java - 使用 Jackson 无法识别 @JsonCreator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29993804/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com