2015-11-06 74 views
0

當我嘗試使用POST向我的Jersey REST服務傳遞JSON字符串時,REST客戶端返回415 - 不支持的媒體類型。通過GET查詢資源工作正常。傑克遜不會將JSON字符串序列化爲Java對象

實體類:

@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:

@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回報我TESTDATA:

[{"id":2104,"name":"pi3"},{"id":2107,"name":"pi4"}] 

但是,當我嘗試使用POST將JSON字符串傳遞到http://localhost:8080/simulation_api/agents/create時,如上所述發生錯誤(HTTP響應415)。我把要求的身體內:

{"id":2804,"name":"pi7"} 

可能是什麼問題?沒有拋出異常......

+2

你是否包括「內容類型:應用程序/ JSON的」頭在你的POST請求? –

回答

0

@Don波特斯坦 是的,如果我通過捲曲發送POST請求:

curl -X POST -H "Content-Type: application/json" -d {"id":2804 
,"name":"pi7"} http://localhost:8080/simulation_api/agents/create -v 

然後捲曲的回報:

* 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類手動將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); 
} 
} 
+0

這是由於請求中的內容類型不正確引起的,我想。我有同樣的問題,我可以看到,在數據包發送領域被打包成一個字符串... http://stackoverflow.com/questions/43547144/multipart-nodejs-request-with-object-in-it 必須有強制傑克遜自動進行這種轉換的方法。 – greengold