如何将文件上传到java中的restful Web服务?

时间:2015-10-09 04:27:42

标签: java web-services rest maven jax-rs

我很擅长在java中构建REST Web服务(在.NET世界中构建了几个),我遇到了一个问题,我的google-fu也没有找到答案。我有一个帖子方法:

@POST
@Consumes(value = MediaType.MULTIPART_FORM_DATA)
public Response postFile(@FormDataParam("file") InputStream content,
        @FormDataParam("file") FormDataContentDisposition fileDetail) {
    String fileName = fileDetail.getName();
    String ext = getFileExtension(fileName);
    URI uri = context.getBaseUri();

    UUID userId = processData.SubmitData(content, ext);

    return Response.created(URI.create(uri + "/api/appuser/" + userId)).build();
}

当我尝试发布时,它返回404。我使用的是最新版本的Jax-rs,玻璃鱼等。

我的web.xml文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>ScoringApp</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

    <servlet>
        <servlet-name>Scoring Web Application</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <!-- Enable Tracing support. -->
        <init-param>
            <param-name>jersey.config.server.tracing</param-name>
            <param-value>ALL</param-value>
        </init-param>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>com.scoringapp.api</param-value>
        </init-param>
        <init-param>
           <param-name>javax.ws.rs.Application</param-name>
           <param-value>com.scoringapp.api.MyApplication</param-value>
       </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Scoring Web Application</servlet-name>
        <url-pattern>/api/*</url-pattern>
    </servlet-mapping>
</web-app>

我的应用程序类:

public class MyApplication extends Application {
    @Override
    public Set<Class<?>> getClasses() {
        final Set<Class<?>> classes = new HashSet<Class<?>>();
        // register resources and features
        classes.add(MultiPartFeature.class);
        classes.add(FileResource.class);
        classes.add(LoggingFilter.class);
        classes.add(CORSResponseFilter.class);
        classes.add(CORSRequestFilter.class);
        return classes;
    }
}

其他任何可以帮我解决这个问题的事情都会很棒。 (在Maven中运行,使用动态Web应用程序,我按照教程:)有几个其他测试端点可以正常工作,所以我知道我可以访问它并点击基本url返回index.html我创造的只是为了确保一切都在那里。)

1 个答案:

答案 0 :(得分:0)

看起来我的问题没有正确遵循HTML规范并且正在观看我的URI外壳。我的坏......

相关问题