Java 如何编写 Jersey Multipart webapp,Tomcat 服务器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22837257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 18:14:48  来源:igfitidea点击:

How to write Jersey Multipart webapp, Tomcat Server

javaresttomcatnetbeans

提问by Akin_Glen

I've been doing a lot of REST tutorials and enjoying them. Recently, I tried writing a jersey multipart webapp with Netbeans but I can't seem to because it seems something's missing my jersey library.

我一直在做很多 REST 教程并享受它们。最近,我尝试用 Netbeans 编写一个 jersey multipart webapp,但我似乎不能,因为我的 jersey 库似乎缺少某些东西。

I downloaded the jersey-multipart.jar file, but still that didn't help:

我下载了 jersey-multipart.jar 文件,但仍然没有帮助:

@Path("/file")
public class UploadFileService {

    @POST
    @Path("/upload")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public Response uploadFile(
        @FormDataParam("file") InputStream uploadedInputStream,
        @FormDataParam("file") FormDataContentDisposition fileDetail) {

This code is from blog. I'm trying to put it in my webapp, but the @FormDataParam tag and the FormDataContentDisposition class are not recognised. I downloaded the jersey-multipart.jar and that seemed to solve the @FormDataParam tag problem, but not the FormDataContentDisposition class.

此代码来自博客。我试图将它放在我的 web 应用程序中,但无法识别 @FormDataParam 标记和 FormDataContentDisposition 类。我下载了 jersey-multipart.jar,这似乎解决了 @FormDataParam 标签问题,但不是 FormDataContentDisposition 类。

I'm using Tomcat 7.0.

我正在使用 Tomcat 7.0。

How do I go about successfully creating a jersey multipart webapp without any problems? And how come the jersey-multipart jar file isn't included in the jersey library in Netbeans?

我如何成功地创建一个 jersey multipart webapp 而没有任何问题?为什么在 Netbeans 的 jersey 库中不包含 jersey-multipart jar 文件?

Thanks.

谢谢。

采纳答案by Akin_Glen

Lutz Horn has a point, but for the sake of those using Netbeans 7.4 (Java EE 6) and are still struggling with this issue, here's a step by step on how to create your very own multipart rest web service and deploying on Tomcat, with Netbeans. (Note, deploying on Glassfish requires a slightly different configuration which isn't covered in this answer).

Lutz Horn 有一个观点,但为了那些使用 Netbeans 7.4 (Java EE 6) 并且仍在努力解决这个问题的人,这里有一个关于如何创建你自己的 multipart rest web 服务并在 Tomcat 上部署的分步指南,网豆。(请注意,在 Glassfish 上部署需要稍微不同的配置,本答案未涵盖)。

First off, my suggestion is to create a maven web application and not a normal web application. Reason is, the JAX-RS and Jersey libraries that come with Java EE 6 are not sufficient enough, and once you start fiddling around with external jars, things tend to get messy, especially with Jersey. (Hopefully, this has been corrected in Netbeans 8.0 (Java EE 7)).

首先,我的建议是创建一个 Maven Web 应用程序,而不是一个普通的 Web 应用程序。原因是,Java EE 6 附带的 JAX-RS 和 Jersey 库还不够,一旦您开始摆弄外部 jar,事情往往会变得混乱,尤其是 Jersey。(希望这已在 Netbeans 8.0 (Java EE 7) 中得到纠正)。

(1) Create a maven web-app, choose Java EE 6 and Tomcat 7. Once you're done, you'll notice you don't have a web.xml. Most multipart tutorials will tell you to include certain configurations in your web.xml file. Don't bother with that. You don't need a web.xml file.

(1) 创建一个maven web-app,选择Java EE 6 和Tomcat 7。完成后,你会发现你没有web.xml。大多数多部分教程会告诉您在 web.xml 文件中包含某些配置。别管那个。您不需要 web.xml 文件。

(2) Create a RESTfull web service by either writing it manually or using the wizard (right click on your maven web-app -- New -- Other -- Web Services -- [choose the RESTful web service you want])

(2) 通过手动编写或使用向导来创建 RESTful web 服务(右键单击您的 maven web-app -- New -- Other -- Web Services -- [选择你想要的 RESTful web service])

(3) Open your pom.xml (you can find it under the Project Filesfolder in your maven web-app) and add these dependencies:

(3) 打开你的 pom.xml(你可以在你的 maven web-app的Project Files 文件夹下找到它)并添加这些依赖项:

        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <version>2.7</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-multipart</artifactId>
            <version>2.7</version>
        </dependency>

If you're doing this for the first time, you need an active internet connection, as maven will download the dependencies from its central repository.

如果您是第一次这样做,您需要一个有效的互联网连接,因为 maven 将从其中央存储库下载依赖项。

(4) Go to your ApplicationConfigclass or whatever class that holds that contains your @ApplicationPath(). It should look like this:

(4) 转到您的ApplicationConfig类或任何包含您的 @ApplicationPath() 的类。它应该是这样的:

@javax.ws.rs.ApplicationPath("webresources")
public class ApplicationConfig extends Application {
    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> resources = new java.util.HashSet<Class<?>>();
        resources.add(MultiPartFeature.class);
        addRestResourceClasses(resources);
        return resources;
    }

    /**
     * Do not modify addRestResourceClasses() method.
     * It is automatically populated with
     * all resources defined in the project.
     * If required, comment out calling this method in getClasses().
     */
    private void addRestResourceClasses(Set<Class<?>> resources) {
        resources.add(com.mycompany.mavenrestuploader.UploaderResource.class);
    }

Note: resources.add(MultiPartFeature.class);That has to be included, otherwise Jersey multipart won't work.

注意:resources.add(MultiPartFeature.class); 这必须包括在内,否则 Jersey multipart 将不起作用。

The reason I put that line of code in the getClasses method and not the addRestResourceClasses method is because the addRestResourceClasses method gets modified whenever there's a change to your resource class, and if you include the MultiPartFeature code in there, it will get erased.

我将那行代码放在 getClasses 方法而不是 addRestResourceClasses 方法中的原因是因为只要您的资源类发生更改,addRestResourceClasses 方法就会被修改,如果您在其中包含 MultiPartFeature 代码,它将被删除。

Once you've done all these things, you are good to go.

一旦你完成了所有这些事情,你就可以开始了。

If you're just looking to create a RESTful web service without multipart, follow steps 1 to 3, but in step 3 do not include the jersey-media-multipartdependency.

如果您只是想创建一个没有 multipart 的 RESTful Web 服务,请按照步骤 1 到 3,但在步骤 3 中不要包含jersey-media-multipart依赖项。

I hope this helps you ;)

我希望这可以帮助你 ;)

回答by Akin_Glen

The imports for these two are

这两个的进口是

import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataParam;

If you use Maven, add this dependencies:

如果您使用 Maven,请添加以下依赖项:

    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-multipart</artifactId>
        <version>2.0</version>
        <type>jar</type>
    </dependency>

回答by Grigory Kislin

Together with jersey-media-multipartdependency, instead of Application(see below) you can configure ResourceConfig:

jersey-media-multipart依赖项一起,Application您可以配置而不是(见下文)ResourceConfig

@ApplicationPath("/")
public class AppConfig extends ResourceConfig {
    public AppConfig() {
        packages("packages.to.scan");
        register(MultiPartFeature.class);
    }
}

or Jersey REST configuration in web.xml:

或 Jersey REST 配置web.xml

<init-param>
    <param-name>jersey.config.server.provider.classnames</param-name>
    <param-value>org.glassfish.jersey.media.multipart.MultiPartFeature</param-value>
</init-param>