如何编写 Jersey Multipart webapp,Tomcat 服务器 [英] How to write Jersey Multipart webapp, Tomcat Server

查看:23
本文介绍了如何编写 Jersey Multipart webapp,Tomcat 服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

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

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

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

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.

我使用的是 Tomcat 7.0.

I'm using Tomcat 7.0.

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

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?

谢谢.

推荐答案

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

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).

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

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)).

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

(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.

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

(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])

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

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

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

如果您是第一次执行此操作,则需要有效的互联网连接,因为 maven 将从其中央存储库下载依赖项.

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.

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

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

@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);
    }

注意:resources.add(MultiPartFeature.class); 必须包括在内,否则 Jersey multipart 将无法工作.

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

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

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.

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

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

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

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-multipart dependency.

希望对你有帮助;)

这篇关于如何编写 Jersey Multipart webapp,Tomcat 服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆