也許有人可以指出我的方向是正確的,我潛伏了太久的解決方案..使用Jersey從一個JSON創建多個JPA持久實體?
我使用球衣從POST數據獲取JSON發送到REST服務,並創建一個持久性實體插入我的數據庫。像這樣的東西:
@POST
@Path("/create")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response createRecord(Element element) throws Exception{
violations = validator.validate(element);
if (violations.size() > 0) {
jsonResponse.setSuccess(false);
jsonResponse.setMessage("TODO error message");
jsonResponse.setValidationError(this.getValidationErrorList());
}
else {
try {
em.persist(element);
em.flush();
}
catch (PersistenceException e) {
System.out.println(e.getMessage());
jsonResponse.setSuccess(false);
jsonResponse.setMessage("TODO error");
}
}
jsonResponse.setSuccess(true);
jsonResponse.setMessage("Success!");
return Response.ok().entity(new ObjectMapper().writeValueAsString(jsonResponse)).build();
}
但是我想從一個JSON構建多個實體是這樣的:
:[
{
"usrcre": 0,
"usrmod": 0,
"datmod": null,
"vaecleint": 40,
"vaeelecle": "1fdsfds",
"vaevalcle": "fdsf",
"vaedsc": "dsfdsf"
},
{
"usrcre": 0,
"usrmod": 0,
"datmod": null,
"vaecleint": 41,
"vaeelecle": "1TEST",
"vaevalcle": "0003",
"vaedsc": "fdgfdgfdg"
},
{
"usrcre": 0,
"usrmod": 0,
"datmod": null,
"vaecleint": 42,
"vaeelecle": "1TEST",
"vaevalcle": "0004",
"vaedsc": "Valeur 0004gfdgfdg"
},
{
"usrcre": 0,
"usrmod": 0,
"datmod": null,
"vaecleint": 43,
"vaeelecle": "1TEST",
"vaevalcle": "0005",
"vaedsc": "Valeur 0005fdgfdgd"
}
]
心中已經使用類似的嘗試
public Response createRecord(List<Element> elements) throws Exception
然後嘗試迭代這個列表並堅持每個元素,但我得到一個:
java.lang.IllegalArgumentException: argument type mismatch
只要我的方法簽名包含列表或數組。我錯過了什麼嗎?
更新:
這裏,當我試圖訪問列表(我使用的堆棧跟蹤「爲每個」,試圖在上述列表堅持每一個元素,但它似乎總是有一個空元素無論我怎麼把我的JSON)
Warning: StandardWrapperValve[Jersey Web Application]: Servlet.service() for
servlet Jersey Web Application threw exception
java.lang.IllegalArgumentException: argument type mismatch
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:125)
at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$ResponseOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:152)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:91)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:346)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:341)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:101)
at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:224)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:198)
at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:946)
at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:323)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:372)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:335)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:218)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
at java.lang.Thread.run(Thread.java:744)
編輯:或者,如果有人有不介入解析JSON收到創建集合「元素」一個好的工作環境的想法,我會所有的來信!
你可以發佈完整的堆棧跟蹤嗎? – fge
用完整的堆棧跟蹤進行編輯! – alex