In Spring Roo 2.0, how can I send a binary file using multipart/form-data

时间:2017-06-19 13:56:23

标签: json jersey spring-roo

I have a class that needs to store a binary file, so I have field of type file:

entity jpa --class ~.model.Attachment
field string --fieldName description
field string --fieldName createdBy
field enum --fieldName attachmentType –type ~.model.attachmentTypesEnum
field string --fieldName fileName
field file --fieldName attachmentFilei --contentType ZIP --autoUpload --notNull false

Now I need to have my mobile application upload a binary file. I realize I could use Base64 encoding to convert it to a printable string, but the files may be quite large so I would prefer to use multipart/form-data to send binary data. I do not need the binary file stored in the mysql database since I would rather put it in an S3 bucket in AWS. So I can send the binary file separately, return its location to the client who can then send the Attachment record as JSON referencing the location in S3.

field string --fieldName bucketId
field string --fieldName S3URL

I have found numerous discussions of this topic that suggest using Jersey to send the file. But missing that code in my Spring Roo project crashes maven when I try to compile. I think it does not like the @Path line.

ERROR] execute error
java.util.concurrent.ExecutionException: java.lang.RuntimeException: java.lang.NullPointerException
at java.util.concurrent.FutureTask.report(FutureTask.java:122)
at java.util.concurrent.FutureTask.get(FutureTask.java:188)
at com.mysema.maven.apt.AbstractProcessorMojo.execute(AbstractProcessorMojo.java:346)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:133)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:108)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:76)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:116)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:361)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:155)
at org.apache.maven.cli.MavenCli.execute(MavenCli.java:584)
at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:213)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:157)
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.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
Caused by: java.lang.RuntimeException: java.lang.NullPointerException
at com.sun.tools.javac.main.Main.compile(Main.java:469)
at com.sun.tools.javac.api.JavacTaskImpl.call(JavacTaskImpl.java:132)
at com.sun.tools.javac.api.JavacTaskImpl.call(JavacTaskImpl.java:67)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.NullPointerException
at com.querydsl.codegen.SimpleSerializerConfig.getConfig(SimpleSerializerConfig.java:30)
at com.querydsl.apt.DefaultConfiguration.<init>(DefaultConfiguration.java:112)
at com.querydsl.apt.jpa.JPAConfiguration.<init>(JPAConfiguration.java:54)
at org.springframework.roo.querydsl.processor.RooAnnotationProcessor.createConfiguration(RooAnnotationProcessor.java:37)
at com.querydsl.apt.AbstractQuerydslProcessor.process(AbstractQuerydslProcessor.java:79)
at com.sun.tools.javac.processing.JavacProcessingEnvironment.callProcessor(JavacProcessingEnvironment.java:793)
at com.sun.tools.javac.processing.JavacProcessingEnvironment.discoverAndRunProcs(JavacProcessingEnvironment.java:722)
at com.sun.tools.javac.processing.JavacProcessingEnvironment.access$1700(JavacProcessingEnvironment.java:97)
at com.sun.tools.javac.processing.JavacProcessingEnvironment$Round.run(JavacProcessingEnvironment.java:1029)
at com.sun.tools.javac.processing.JavacProcessingEnvironment.doProcessing(JavacProcessingEnvironment.java:1163)
at com.sun.tools.javac.main.JavaCompiler.processAnnotations(JavaCompiler.java:1108)
at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:824)
at com.sun.tools.javac.main.Main.compile(Main.java:439)
... 6 more
[INFO] -----------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------

Can you point me to any documentation or example code for sending binary files to my Spring Roo project?

1 个答案:

答案 0 :(得分:0)

由于我想在AWS S3中存储文件并且不想基于64对它们进行编码,因此我发现在http://www.mkyong.com/spring-boot/spring-boot-file-upload-example/上传二进制文件的答案非常好。它工作得很好,我使用JSON将有关文件的信息传递给Attachment类。