Jackson 没有将 JSON 字符串序列化为 Java 对象
Jackson doesn't serialize JSON String to Java Object
当我尝试使用 POST 将 JSON-String 传递到我的 Jersey REST 服务时,REST 客户端 returns 415 - 不支持的媒体类型。通过 GET 查询资源工作正常。
实体class:
@Entity
public class Agent implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
public Agent() {
}
public Agent(String name) {
this.name = name;
}
@OneToMany(mappedBy = "agent", cascade = CascadeType.PERSIST)
@JsonBackReference
private List<Action> actions = new ArrayList<>();
//...getter / setter...
}
REST 服务 class:
@Path("/agents")
public class AgentService {
private static PersistenceFacade f = new PersistenceFacade();
private static StatusFacade s = new StatusFacade();
//GET all Agents
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Agent> getAllAgents() {
return f.getAllAgents();
}
@POST
@Path("/create")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public void postAgent(Agent a) {
f.createAgent(a);
}
}
PersistenceFacade
public class PersistenceFacade {
//EMF als zentraler Handler für alle EM
private static EntityManagerFactory factory = Persistence.createEntityManagerFactory("simulationPU");
public void createAgent(Agent a) {
EntityManager em = factory.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
em.persist(a);
tx.commit();
}
public List<Agent> getAllAgents() {
EntityManager em = factory.createEntityManager();
Query q = em.createQuery("SELECT a FROM Agent a");
List<Agent> aList = q.getResultList();
return aList;
}
正在将 GET 请求传递给 http://localhost:8080/simulation_api/agents returns 我的测试数据:
[{"id":2104,"name":"pi3"},{"id":2107,"name":"pi4"}]
但是,当我尝试使用 POST 将 JSON-String 传递给 http://localhost:8080/simulation_api/agents/create 时,会出现上述错误(HTTP 响应 415)。我在请求体里面放了:
{"id":2804,"name":"pi7"}
可能是什么问题?没有抛出异常...
@Don Bottstein
是的,如果我通过 curl 发送 POST-Request:
curl -X POST -H "Content-Type: application/json" -d {"id":2804
,"name":"pi7"} http://localhost:8080/simulation_api/agents/create -v
然后卷曲 returns:
* Trying ::1...
* Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> POST /simulation_api/agents/create HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.45.0
> Accept: */*
> Content-Type: application/json
> Content-Length: 18
>
* upload completely sent off: 18 out of 18 bytes
< HTTP/1.1 500 Request failed.
< Content-Type: text/html;charset=ISO-8859-1
< Date: Sat, 07 Nov 2015 09:26:07 GMT
< Connection: close
< Content-Length: 1033
<
编辑:解决方案
我发现,有必要通过 ObjectMapper class 手动将 JSON 输入字符串转换为对象。 (我认为@Consumes 接口在后台执行此操作...)
现在可以使用了:
@Path("/agents")
public class AgentService {
private static PersistenceFacade f = new PersistenceFacade();
private static StatusFacade s = new StatusFacade();
//GET all Agents
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Agent> getAllAgents() {
return f.getAllAgents();
}
@POST
@Path("/create")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public void postAgent(String content) throws IOException {
ObjectMapper mapper = new ObjectMapper();
Agent a = mapper.readValue(content, Agent.class);
f.createAgent(a);
}
}
当我尝试使用 POST 将 JSON-String 传递到我的 Jersey REST 服务时,REST 客户端 returns 415 - 不支持的媒体类型。通过 GET 查询资源工作正常。
实体class:
@Entity
public class Agent implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
public Agent() {
}
public Agent(String name) {
this.name = name;
}
@OneToMany(mappedBy = "agent", cascade = CascadeType.PERSIST)
@JsonBackReference
private List<Action> actions = new ArrayList<>();
//...getter / setter...
}
REST 服务 class:
@Path("/agents")
public class AgentService {
private static PersistenceFacade f = new PersistenceFacade();
private static StatusFacade s = new StatusFacade();
//GET all Agents
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Agent> getAllAgents() {
return f.getAllAgents();
}
@POST
@Path("/create")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public void postAgent(Agent a) {
f.createAgent(a);
}
}
PersistenceFacade
public class PersistenceFacade {
//EMF als zentraler Handler für alle EM
private static EntityManagerFactory factory = Persistence.createEntityManagerFactory("simulationPU");
public void createAgent(Agent a) {
EntityManager em = factory.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
em.persist(a);
tx.commit();
}
public List<Agent> getAllAgents() {
EntityManager em = factory.createEntityManager();
Query q = em.createQuery("SELECT a FROM Agent a");
List<Agent> aList = q.getResultList();
return aList;
}
正在将 GET 请求传递给 http://localhost:8080/simulation_api/agents returns 我的测试数据:
[{"id":2104,"name":"pi3"},{"id":2107,"name":"pi4"}]
但是,当我尝试使用 POST 将 JSON-String 传递给 http://localhost:8080/simulation_api/agents/create 时,会出现上述错误(HTTP 响应 415)。我在请求体里面放了:
{"id":2804,"name":"pi7"}
可能是什么问题?没有抛出异常...
@Don Bottstein 是的,如果我通过 curl 发送 POST-Request:
curl -X POST -H "Content-Type: application/json" -d {"id":2804
,"name":"pi7"} http://localhost:8080/simulation_api/agents/create -v
然后卷曲 returns:
* Trying ::1...
* Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> POST /simulation_api/agents/create HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.45.0
> Accept: */*
> Content-Type: application/json
> Content-Length: 18
>
* upload completely sent off: 18 out of 18 bytes
< HTTP/1.1 500 Request failed.
< Content-Type: text/html;charset=ISO-8859-1
< Date: Sat, 07 Nov 2015 09:26:07 GMT
< Connection: close
< Content-Length: 1033
<
编辑:解决方案
我发现,有必要通过 ObjectMapper class 手动将 JSON 输入字符串转换为对象。 (我认为@Consumes 接口在后台执行此操作...)
现在可以使用了:
@Path("/agents")
public class AgentService {
private static PersistenceFacade f = new PersistenceFacade();
private static StatusFacade s = new StatusFacade();
//GET all Agents
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Agent> getAllAgents() {
return f.getAllAgents();
}
@POST
@Path("/create")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public void postAgent(String content) throws IOException {
ObjectMapper mapper = new ObjectMapper();
Agent a = mapper.readValue(content, Agent.class);
f.createAgent(a);
}
}