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

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

问题描述

我一直在做很多REST教程并享受它们。最近,我尝试用Netbeans编写一个球衣多部分webapp,但我似乎无法,因为它似乎缺少我的球衣库。

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

this代码来自博客。我试图将它放在我的webapp中,但@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.

如何成功创建一个没有任何问题的泽西多部分webapp?为什么在Netbeans的球衣库中不包含泽西多部件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)的人并且仍然在努力解决这个问题,这里有一步一步说明如何创建你的非常自己的多部分休息Web服务,并使用 Netbeans 在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库还不够,一旦你开始摆弄外部罐子,事情往往会变得混乱,尤其是泽西岛。 (希望在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)通过手动编写或使用向导创建RESTfull Web服务(右键单击您的maven Web -app - 新建 - 其他 - Web服务 - [选择您想要的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.

如果您只想创建一个没有多部分的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.

我希望这可以帮到你;)

I hope this helps you ;)

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

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