我正在开发一个带有JavaScript前端的RESTful webapp和带有Netbeans 8.2的Jakarta EE后端,到目前为止一直都很好用。我有一个表单,用户上传他的图像,我想将此图像保存为我的数据库中的blob(我知道缺点,但我想以这种方式进行)。请参阅下面的代码段。
首先,表单上的POST请求提交:
var file = URL.createObjectURL(document.getElementById("candidatePicture").files[0]);
fetch(file).then(response => {
response.blob().then(photo => {
const reader = new FileReader();
// Start reading the blob as text.
reader.readAsText(photo);
// This fires after the blob has been read/loaded.
reader.addEventListener('loadend', (e) => {
const text = e.srcElement.result;
var ballotData = $("#addBallotForm").serializeArray();
var ballot = {
"candidateName": ballotData[0].value,
"positionName": ballotData[1].value,
"candidateNIC": ballotData[2].value,
"partyname": ballotData[3].value,
"candidateSlogan": ballotData[4].value,
"candidatePicture": text,
"actionPlan": ""
};
console.log(JSON.stringify(ballot));
$.ajax({
headers: {
'Content-Type': 'application/json'
},
crossDomain: true,
type: "POST",
enctype: 'application/json',
processData: false, // Important! prevent jQuery from transforming the data into a query string
cache: false,
url: url,
data: JSON.stringify(ballot)
}).done((data, textStatus, jqXHR) => {
if (jqXHR.status === 200) {
swal("The ballot has been succesfully created!", {icon: "success"})
.then((confirm) => {
if (confirm) {
table.ajax.reload();
clearAddBallotModal(); // clear the form
$("#addBallotModal").click(); // close the modal
displayBallots(); // redisplay the ballots
}
});
} else {
swal("Oops! Invalid ballot Input", {icon: "warning"});
}
}).fail((jqXHR, textStatus, errorThrown) => {
swal("Error! An error occured!", {icon: "error"});
});
});
});
});
二,资源端点:
@POST
@Path("{ipaddress}/{electionid}")
@ApiOperation(value = "Create new ballot item", notes = "This can only be done by logged in users.")
@ApiResponses(value = {
@ApiResponse(code = 400, message = "Invalid ballot item Input"),
@ApiResponse(code = 200, message = "Ballot item created")})
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response add(
@ApiParam(value = "The ipaddress of the ballot item to be created", required = true) @PathParam("ipaddress") String ip,
@ApiParam(value = "The election's id", required = true) @PathParam("electionid") String electionid,
@ApiParam(value = "The ballot item that needs to be added", required = true) BallotItem newBallot) {
Users curUser = UsersService.find(1l);
Election election = electionService.find(Long.parseLong(electionid));
newBallot.setElectionID(election);
ballotsService.create(newBallot);
operationsService.create(new Operation(curUser, "Created a new ballot item", "Candidate name: " + newBallot.getCandidateName() + " and Election ID: " + electionid, new Date(), ip));
return Response.ok(newBallot).build();
}
我在netbeans控制台中收到以下错误:Severe: javax.ws.rs.ProcessingException: Error deserializing object from entity stream.
javax.ws.rs.ProcessingException: Error deserializing object from entity stream.
at org.glassfish.jersey.jsonb.internal.JsonBindingProvider.readFrom(JsonBindingProvider.java:101)
at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.invokeReadFrom(ReaderInterceptorExecutor.java:257)
at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.aroundReadFrom(ReaderInterceptorExecutor.java:236)
at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor.proceed(ReaderInterceptorExecutor.java:156)
at org.glassfish.jersey.server.internal.MappableExceptionWrapperInterceptor.aroundReadFrom(MappableExceptionWrapperInterceptor.java:73)
at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor.proceed(ReaderInterceptorExecutor.java:156)
at org.glassfish.jersey.message.internal.MessageBodyFactory.readFrom(MessageBodyFactory.java:1093)
at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:874)
at org.glassfish.jersey.server.ContainerRequest.readEntity(ContainerRequest.java:271)
at org.glassfish.jersey.server.internal.inject.EntityParamValueParamProvider$EntityValueSupplier.apply(EntityParamValueParamProvider.java:98)
at org.glassfish.jersey.server.internal.inject.EntityParamValueParamProvider$EntityValueSupplier.apply(EntityParamValueParamProvider.java:81)
at org.glassfish.jersey.server.spi.internal.ParamValueFactoryWithSource.apply(ParamValueFactoryWithSource.java:75)
at org.glassfish.jersey.server.spi.internal.ParameterValueHelper.getParameterValues(ParameterValueHelper.java:93)
at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$AbstractMethodParamInvoker.getParamValues(JavaResourceMethodDispatcherProvider.java:133)
at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$ResponseOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:200)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:103)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:493)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:415)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:104)
at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:277)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:272)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:268)
at org.glassfish.jersey.internal.Errors.process(Errors.java:316)
at org.glassfish.jersey.internal.Errors.process(Errors.java:298)
at org.glassfish.jersey.internal.Errors.process(Errors.java:268)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:289)
at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:256)
at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:704)
at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:416)
at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:370)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:389)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:342)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:229)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1628)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:258)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:755)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:575)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:159)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:371)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:238)
at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:516)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:213)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:182)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:156)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:218)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:95)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:260)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:177)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:109)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:88)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:53)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:524)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:89)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:94)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:33)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:114)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:569)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:549)
at java.lang.Thread.run(Thread.java:745)
Caused by: javax.json.bind.JsonbException: Internal error: Event START_ARRAY not found. Last data: [EVENT: START_OBJECT KEY_NAME: candidatePicture]
at org.eclipse.yasson.internal.JsonbRiParser.moveTo(JsonbRiParser.java:203)
at org.eclipse.yasson.internal.serializer.AbstractArrayDeserializer.moveToFirst(AbstractArrayDeserializer.java:82)
at org.eclipse.yasson.internal.serializer.AbstractContainerDeserializer.deserializeInternal(AbstractContainerDeserializer.java:75)
at org.eclipse.yasson.internal.serializer.AbstractContainerDeserializer.deserialize(AbstractContainerDeserializer.java:61)
at org.eclipse.yasson.internal.serializer.ObjectDeserializer.deserializeNext(ObjectDeserializer.java:165)
at org.eclipse.yasson.internal.serializer.AbstractContainerDeserializer.deserializeInternal(AbstractContainerDeserializer.java:85)
at org.eclipse.yasson.internal.serializer.AbstractContainerDeserializer.deserialize(AbstractContainerDeserializer.java:61)
at org.eclipse.yasson.internal.Unmarshaller.deserializeItem(Unmarshaller.java:62)
at org.eclipse.yasson.internal.Unmarshaller.deserialize(Unmarshaller.java:52)
at org.eclipse.yasson.internal.JsonBinding.deserialize(JsonBinding.java:45)
at org.eclipse.yasson.internal.JsonBinding.fromJson(JsonBinding.java:85)
at org.glassfish.jersey.jsonb.internal.JsonBindingProvider.readFrom(JsonBindingProvider.java:99)
... 60 more
我将'application / json'更改为'mulitpart / form-data',将MediaType.APPLICATION_JSON更改为其他mediatypes,但我得到的是我的浏览器控制台中的415错误作为响应。请问有没有人有想法解决这个问题。提前致谢
JSONB支持反序列化字节数组。问题是您的特定用例。
默认情况下使用BYTE编码。在这种情况下,你的candidatePicture
属性应该是一个数组,看起来类似于:
"candidatePicture":[89,97,115,115,111,110,32,105,115,32,112,101,114,102,101,99,116,33]
我发现这不是你的情况。
如果使用BASE_64编码,这有点标准,您应该告诉JSONB引擎在自定义配置中使用它:
JsonbConfig config = new JsonbConfig()
.withBinaryDataStrategy(BinaryDataStrategy.BASE_64);
Jsonb jsonb = JsonbBuilder.newBuilder()
.withConfig(config)
.build();
jsonb.fromJson(...);
在这种情况下,您的属性应该是BASE64编码的字符串,如下所示:
"candidatePicture":"WWFzc29uIGlzIHBlcmZlY3Qh"
我想这是你的情况,但你的字符串看起来不像BASE64编码。尝试一下,如果不起作用,请检查用于编码二进制数据的编码。
问题是你的BallotItem
类包含字段byte[] candidatePicture
,JSON绑定默认情况下不知道如何从传入的JSON设置。 JSON只包含一个带二进制数据的字符串(我不确定它是否用base64编码)。 Jakarta EE Server尝试将字符串保存到字节数组中并失败。
这可能是您的Jakarta EE服务器中的错误,或者可能未指定。 JSON-Binding规范http://json-b.net/没有提到它应该如何工作。它仅指定默认情况下使用“byte”策略将字节数组转换为JSON,但没有提及如何从JSON转换二进制数据。
我建议用candidatePicture
在BallotItem
中为你的JsonbTypeDeserializer字段添加一个自定义反序列化器。这应该工作,但需要几行代码来实现。