我有一個現有的Web應用程序,包括jsps和servlet,我試圖添加SpringFramework Boot以便我可以嚮應用程序添加一些新的簡單休息服務。這些其餘的服務通過jackson-databind接受和返回JSON。SpringFramework Boot在其他servlet讀取它之前讀取輸入流
問題是,在這樣做的時候,現有的servlet會讀取上傳的文件,它的請求輸入流現在在調用之前消耗,因此文件不在那裏。
使用我加入以下到我的pom.xml中的Maven ...
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>1.2.1.RELEASE</version>
<scope>provided</scope>
</dependency>
...我已經添加了以下RestApplication類...
@Configuration
@EnableAutoConfiguration
@ComponentScan
public class RestApplication extends SpringBootServletInitializer {
public static void main(final String[] args) {
SpringApplication.run(RestApplication.class, args);
}
@Override
protected final SpringApplicationBuilder configure(final SpringApplicationBuilder application) {
return application.sources(RestApplication.class);
}
}
...和下面的控制器類...
@RestController
@RequestMapping(value = "/obj/mycontroller")
public class MyController
{
@RequestMapping(value = "/save", method = RequestMethod.POST)
public long save(final HttpServletRequest request,
final MyObject thing)
{
// save the thing
}
}
...這一切工作正常,但現在我的文件上傳標準servlet已經壞了。它仍然被調用,但它的輸入流看起來是空的,所以沒有找到文件。這是servlet的定義...
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
@MultipartConfig
public class MediaUploadServlet
extends HttpServlet
{
protected void doPost(final HttpServletRequest request,
final HttpServletResponse response)
throws ServletException, IOException
{
if (ServletFileUpload.isMultipartContent(request)) {
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
try {
List<FileItem> items = upload.parseRequest(request);
// do stuff with the files.
} catch (FileUploadException ex) {
// handle error
}
}
}
}
...和項目現在是空的。
如果我拿走@EnableAutoConfiguration標記,則MediaUploadServlet再次工作,但MyController不能。所以我的問題是,我怎樣才能配置彈簧啓動到只有在MyController上做它的魔術或而不是在MediaUploadServlet上做它的魔力?
註冊您的輔助的servlet是這樣的: http://stackoverflow.com/questions/20915528/我怎麼能註冊一個輔助servlet-with-spring-boot – PaulNUK 2015-04-02 20:50:03
是的,就是這樣。謝謝! – crowmagnumb 2015-04-14 20:13:39
如果你以我的方式回答,我可以投票選出你的答案並刪除我的答案。 – crowmagnumb 2015-04-15 21:32:25