2013-08-02 13 views
6

を使用して桟橋からサーブレットエラーなしmultipartconfig:私はユニットテストにアップロード通話をしようとしていますが、私は次のコードのために、このエラーが出るscalatra

@MultipartConfig(maxFileSize = 3145728) 
class WebServlet extends ScalatraServlet with FileUploadSupport { 
    override def isSizeConstraintException(e: Exception) = e match { 
    case se: ServletException if se.getMessage.contains("exceeds max filesize") || 
     se.getMessage.startsWith("Request exceeds maxRequestSize") => true 
    case _ => false 
    } 
    error { 
    case e: SizeConstraintExceededException => RequestEntityTooLarge("too much!") 
    } 
    post("/uploadscript") { 
    val privateParam = try {params("private") != null && params("private").equals("true") } catch { case _ => false } 
    println("privateParam = " + privateParam) 
    val file = fileParams("file") 
    println(s"The size of the file is ${file.size}") 
    } 

エラーは次のとおりです。

java.lang.IllegalStateException: No multipart config for servlet 
    at org.eclipse.jetty.server.Request.getParts(Request.java:2064) ~[jetty-server-8.1.10.v20130312.jar:8.1.10.v20130312] 
    at org.scalatra.servlet.FileUploadSupport$class.getParts(FileUploadSupport.scala:133) ~[scalatra_2.10-2.2.1.jar:2.2.1] 
    at org.scalatra.servlet.FileUploadSupport$class.extractMultipartParams(FileUploadSupport.scala:108) ~[scalatra_2.10-2.2.1.jar:2.2.1] 
    at org.scalatra.servlet.FileUploadSupport$class.handle(FileUploadSupport.scala:79) ~[scalatra_2.10-2.2.1.jar:2.2.1] 
    at com.ui.WebServlet.handle(WebServlet.scala:32) ~[classes/:na] 

そして、これは私の単体テストで、最初のものが成功したので、私のWebサービスを見つけることになります:

class WebServletSpecification extends MutableScalatraSpec { 
    addServlet(classOf[WebServlet], "/*") 

    "GET /hello" should { 
    "return status 200" in { 
     get("/hello/testcomputer") { 
     status must_== 200 
     } 
    } 
    } 
    "POST /uploadscript" should { 
    "return status 200" in { 
    val scriptfile = "testfile" 
    val scriptname = "basescript" 
     post("/uploadscript", Map("private" -> "true"), Map("file" -> new File(scriptfile))) { 
     status must_== 200 
     } 
    } 
    } 
} 

Eclipseの内部にあり、何が起こっているのかはわかりません。

HttpPostMultipartEntityを使ってもうまく動作するので、Eclipseやスケラトラ仕様フレームワークの動作に問題があるようです。

何が間違っている可能性がありますか?

別にweb.xmlはありません。

"org.eclipse.jetty" % "桟橋-webappの" %「8.1.10.v20130312:私はbuild.sbtに使用するものから見て、私は唯一の桟橋8.1.10を使用しています

、 「% "コンテナ"

答えて

6

WebAppContextではなくServletContextHandlerを使用しているときに見つけた解決策を投稿してください。ここから:https://bugs.eclipse.org/bugs/show_bug.cgi?id=395000

import org.eclipse.jetty.server.Handler; 
import org.eclipse.jetty.server.Server; 
import org.eclipse.jetty.server.handler.HandlerList; 
import org.eclipse.jetty.servlet.ServletContextHandler; 
import org.eclipse.jetty.servlet.ServletHolder; 
import javax.servlet.MultipartConfigElement; 

public class WebServer { 

    protected Server server; 

    public static void main(String[] args) throws Exception { 
     int port = 8080; 
     Server server = new Server(port); 

     ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS); 
     context.setContextPath("/"); 


     ServletHolder fileUploadServletHolder = new ServletHolder(new FileUploadServlet()); 
     fileUploadServletHolder.getRegistration().setMultipartConfig(new MultipartConfigElement("data/tmp")); 
     context.addServlet(fileUploadServletHolder, "/fileUpload"); 

     server.setHandler(context); 
     server.start(); 
     server.join(); 
    } 
} 
1

@MultipartConfigは、サーブレット仕様3.0注釈です。 Jetty環境でアノテーションをサポートするために、適切な成果物と設定を追加する必要があります。

jetty-annotationsjetty-plusのアーティファクトが必要です。

次に、テストサーバーを適切な構成でセットアップする必要があります。

このような

...

(私は申し訳ありませんが、ここではScalaの詳細を知らない)

package com.company.foo; 

import org.eclipse.jetty.annotations.AnnotationConfiguration; 
import org.eclipse.jetty.plus.webapp.EnvConfiguration; 
import org.eclipse.jetty.plus.webapp.PlusConfiguration; 
import org.eclipse.jetty.server.Server; 
import org.eclipse.jetty.webapp.Configuration; 
import org.eclipse.jetty.webapp.FragmentConfiguration; 
import org.eclipse.jetty.webapp.MetaInfConfiguration; 
import org.eclipse.jetty.webapp.TagLibConfiguration; 
import org.eclipse.jetty.webapp.WebAppContext; 
import org.eclipse.jetty.webapp.WebInfConfiguration; 
import org.eclipse.jetty.webapp.WebXmlConfiguration; 

public class EmbedMe { 
    public static void main(String[] args) throws Exception { 
     int port = 8080; 
     Server server = new Server(port); 

     String wardir = "target/sample-webapp-1-SNAPSHOT"; 

     WebAppContext context = new WebAppContext(); 
     context.setResourceBase(wardir); 
     context.setDescriptor(wardir + "WEB-INF/web.xml"); 
     context.setConfigurations(new Configuration[] { 
       new AnnotationConfiguration(), new WebXmlConfiguration(), 
       new WebInfConfiguration(), new TagLibConfiguration(), 
       new PlusConfiguration(), new MetaInfConfiguration(), 
       new FragmentConfiguration(), new EnvConfiguration() }); 

     context.setContextPath("/"); 
     context.setParentLoaderPriority(true); 
     server.setHandler(context); 
     server.start(); 
     server.join(); 
    } 
} 

これはhttps://github.com/jetty-project/embedded-servlet-3.0例プロジェクトからです。

+0

ありがとうございます。私が言及したように、私がsbt(シンプルビルドツール)から起動すると、ウェブサーバに行くjunitテストをいつ使用するか分かります。しかし、私がspecs2を使用しようとすると、それがEclipseで実行されていると仮定していますが、そうではありません。私はこの単体テストのプログラムを変更して、それがなぜ機能しないのか分かりません。これも私のためにコンパイルされません –

5

誰が突堤9で、この探している場合:

request.handle(...)にこれを追加

MultipartConfigElement multipartConfigElement = new MultipartConfigElement((String)null); 
request.setAttribute(Request.__MULTIPART_CONFIG_ELEMENT, multipartConfigElement); 
+0

は、__MULTIPART_CONFIG_ELEMENTは、私は私の桟橋版としてこれを持っている(私は:)あまりにinterwebsに時代遅れのものを憎む)私はちょうどチェックし、未知の分野 – SRedouane

+0

です: '' ' \t \t org.eclipse.jetty \t \t 桟橋 - サーバー \t \t 9.3.8.v20160314 \t '' ' – Richard

関連する問題