从Tomcat提供文件时上传错误请求,但从mvn tomcat7:run提供时工作正常

时间:2014-11-30 16:48:07

标签: spring-mvc

我有一个带有Spring REST api的AngularJS应用程序。

AngularJS静态资源可以由grunt serve命令或Apache服务器或Tomcat本身提供。

可以通过独立Tomcat上的手动部署或通过mvn tomcat7:run命令来提供api。

Tomcat的版本是7。

所有这些命令都在同一个Linux用户下执行。

有一个Spring控制器文件上传处理程序。

从grunt serve + Tomcat提供服务时失败,从grunt build + Tomcat服务失败,从Apache + Tomcat服务失败

从grunt serve + mvn tomcat7:run服务时工作正常,并且从grunt服务器运行时工作正常:dist + mvn tomcat7:从apache + mvn tomcat7运行并运行正常:运行

以上所有命令都在我的开发服务器上完成。

它也在生产服务器的Tomcat上以相同的方式失败。

我知道grunt构建正在完成它的工作。

Chromium和Firefox中的错误相同。

从控制器处理程序中删除方法= RequestMethod.POST注释并没有改变控制台错误输出中的任何内容。

问题应该由服务器端决定......错误信息是:请求方法' POST'不支持

Chromium浏览器中显示的失败请求:

Remote Address:127.0.0.1:8080
Request URL:http://localhost:8080/nitro-manager-rest/api/rollouts/import
Request Method:POST
Status Code:400 Bad Request
Request Headersview source
Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8,fr;q=0.6,es;q=0.4,et;q=0.2,nb;q=0.2,ru;q=0.2,sv;q=0.2,it;q=0.2,de;q=0.2
Connection:keep-alive
Content-Length:2389
Content-Type:multipart/form-data; boundary=----WebKitFormBoundarykinGH3Nf0BBnRvRW
Host:localhost:8080
Origin:http://localhost:9000
Referer:http://localhost:9000/
User-Agent:Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/34.0.1847.116 Chrome/34.0.1847.116 Safari/537.36
X-Auth-Token:eyJhbGciOiJIUzI1NiJ9.eyJleHAiOjE0MTc4NjY2NjAsInN1YiI6Im5zbkBuc24uY29tIn0.OhfS2HFFjXC64Ql7h6--PkrUvFbBis8CiFPb4HGn1_k
Request Payload
------WebKitFormBoundarykinGH3Nf0BBnRvRW
Content-Disposition: form-data; name="file"; filename="SFR-rollout-rollout-export.zip"
Content-Type: application/zip


------WebKitFormBoundarykinGH3Nf0BBnRvRW--
Response Headersview source
Access-Control-Allow-Credentials:true
Access-Control-Allow-Headers:Accept-Language,Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers,Authorization,X-Filename,Content-Disposition,Content-Length,X-Auth-Token
Access-Control-Allow-Methods:POST, PUT, GET, OPTIONS, DELETE
Access-Control-Allow-Origin:http://localhost:9000
Access-Control-Expose-Headers:Accept-Language,Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers,Authorization,X-Filename,Content-Disposition,Content-Length,X-Auth-Token
Access-Control-Max-Age:3600
Cache-Control:no-cache, no-store, max-age=0, must-revalidate
Connection:close
Content-Type:application/json;charset=UTF-8
Date:Mon, 01 Dec 2014 15:41:04 GMT
Expires:0
Pragma:no-cache
Server:Apache-Coyote/1.1
Transfer-Encoding:chunked
X-Content-Type-Options:nosniff
X-Frame-Options:DENY
X-XSS-Protection:1; mode=block

Chromium浏览器中显示的成功请求:

Remote Address:127.0.0.1:8080
Request URL:http://localhost:8080/nitro-manager-rest/api/rollouts/import
Request Method:POST
Status Code:200 OK
Request Headersview source
Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8,fr;q=0.6,es;q=0.4,et;q=0.2,nb;q=0.2,ru;q=0.2,sv;q=0.2,it;q=0.2,de;q=0.2
Connection:keep-alive
Content-Length:2389
Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryJHnXN3HjJJNYTURZ
Host:localhost:8080
Origin:http://localhost:9000
Referer:http://localhost:9000/
User-Agent:Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/34.0.1847.116 Chrome/34.0.1847.116 Safari/537.36
X-Auth-Token:eyJhbGciOiJIUzI1NiJ9.eyJleHAiOjE0MTc4NjY2NjAsInN1YiI6Im5zbkBuc24uY29tIn0.OhfS2HFFjXC64Ql7h6--PkrUvFbBis8CiFPb4HGn1_k
Request Payload
------WebKitFormBoundaryJHnXN3HjJJNYTURZ
Content-Disposition: form-data; name="file"; filename="SFR-rollout-rollout-export.zip"
Content-Type: application/zip


------WebKitFormBoundaryJHnXN3HjJJNYTURZ--
Response Headersview source
Access-Control-Allow-Credentials:true
Access-Control-Allow-Headers:Accept-Language,Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers,Authorization,X-Filename,Content-Disposition,Content-Length,X-Auth-Token
Access-Control-Allow-Methods:POST, PUT, GET, OPTIONS, DELETE
Access-Control-Allow-Origin:http://localhost:9000
Access-Control-Expose-Headers:Accept-Language,Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers,Authorization,X-Filename,Content-Disposition,Content-Length,X-Auth-Token
Access-Control-Max-Age:3600
Cache-Control:no-store
Cache-Control:no-cache
Content-Type:application/json;charset=UTF-8
Date:Sun, 30 Nov 2014 21:51:40 GMT
Expires:Thu, 01 Jan 1970 00:00:00 GMT
Location:http://localhost:8080/nitro-manager-rest/api/rollouts/import?file=[org.springframework.web.multipart.commons.CommonsMultipartFile@2a52c0]
Pragma:no-cache
Server:Apache-Coyote/1.1
Transfer-Encoding:chunked
X-Content-Type-Options:nosniff
X-Frame-Options:DENY
X-XSS-Protection:1; mode=block

控制器处理程序:

@ProductManager
@Controller
@RequestMapping(RESTConstants.SLASH + RESTConstants.ROLLOUTS)
public class RolloutImportController {

    private static Logger logger = LoggerFactory.getLogger(RolloutImportController.class);

    @Autowired 
    private ResourceService resourceService;

    @Autowired 
    private RolloutService rolloutService;

    @Autowired 
    private ExportService exportService;

    @Autowired
    private RolloutResourceAssembler rolloutResourceAssembler;

    @RequestMapping(value = RESTConstants.SLASH + RESTConstants.SLASH + RESTConstants.IMPORT, method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<List<String>> rolloutImport(@RequestParam(value = "file") List<MultipartFile> files, UriComponentsBuilder builder) {
        logger.debug("==========>> In rolloutImport");
        HttpHeaders responseHeaders = new HttpHeaders();
        List<String> messages = exportService.rolloutImport(files);
        responseHeaders.setLocation(builder.path(RESTConstants.SLASH + RESTConstants.ROLLOUTS + RESTConstants.SLASH + RESTConstants.IMPORT).queryParam("file", files).buildAndExpand().toUri());
        return new ResponseEntity<List<String>>(messages, responseHeaders, HttpStatus.OK);
    }

}

在我的开发服务器上,日志显示:

2014-11-30 17:08:29,967 DEBUG  [DispatcherServlet] DispatcherServlet with name 'NITRo' processing POST request for [/nitro-manager-rest/api/rollouts/import] 
2014-11-30 17:08:30,145 DEBUG  [CommonsMultipartResolver] Found multipart file [file] of size 2194 bytes with original filename [Orange-rollout-rollout-export.zip], stored in memory 
2014-11-30 17:08:30,151 DEBUG  [RequestMappingHandlerMapping] Looking up handler method for path /rollouts/import 
2014-11-30 17:08:30,180 DEBUG  [RequestMappingHandlerMapping] Returning handler method [public org.springframework.http.ResponseEntity<java.util.List<java.lang.String>> com.nsn.nitro.project.rest.controller.RolloutImportController.rolloutImport(java.util.List<org.springframework.web.multipart.MultipartFile>,org.springframework.web.util.UriComponentsBuilder)] 

但在我的生产服务器上它说:

2014-11-30 17:26:35,839 DEBUG  [RequestMappingHandlerMapping] Looking up handler method for path /rollouts/import 
2014-11-30 17:26:35,858 DEBUG  [ExceptionHandlerExceptionResolver] Resolving exception from handler [null]: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported 

编辑:我添加了logback-access输出,控制台日志显示:

POST /nitro-manager-rest/api/rollouts/import HTTP/1.1
host: localhost:8080
connection: keep-alive
content-length: 2389
origin: http://localhost:9000
x-auth-token: eyJhbGciOiJIUzI1NiJ9.eyJleHAiOjE0MTc4NjY2NjAsInN1YiI6Im5zbkBuc24uY29tIn0.OhfS2HFFjXC64Ql7h6--PkrUvFbBis8CiFPb4HGn1_k
user-agent: Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/34.0.1847.116 Chrome/34.0.1847.116 Safari/537.36
content-type: multipart/form-data; boundary=----WebKitFormBoundaryEDbGOJFhgA4Olleb
accept: */*
referer: http://localhost:9000/
accept-encoding: gzip,deflate,sdch
accept-language: en-US,en;q=0.8,fr;q=0.6,es;q=0.4,et;q=0.2,nb;q=0.2,ru;q=0.2,sv;q=0.2,it;q=0.2,de;q=0.2    

HTTP/1.1 400 Bad Request
Access-Control-Expose-Headers: Accept-Language,Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers,Authorization,X-Filename,Content-Disposition,Content-Length,X-Auth-Token
X-XSS-Protection: 1; mode=block
Expires: 0
Access-Control-Max-Age: 3600
Access-Control-Allow-Methods: POST, PUT, GET, OPTIONS, DELETE
Connection: close
Access-Control-Allow-Credentials: true
X-Content-Type-Options: nosniff
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
X-Frame-Options: DENY
Access-Control-Allow-Headers: Accept-Language,Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers,Authorization,X-Filename,Content-Disposition,Content-Length,X-Auth-Token
Date: Mon, 01 Dec 2014 14:37:18 GMT
Transfer-Encoding: chunked
Access-Control-Allow-Origin: http://localhost:9000
Content-Type: application/json;charset=UTF-8

编辑:问题来自处理程序所在的控制器类。将处理程序移动到另一个控制器类可以解决问题。更改控制器类的映射值属性不能解决问题。我希望尽快解决这个问题。

编辑:我可以找到解决问题的方法。这不是解决方案。只是一个解决方案。通过移动处理程序:

@RequestMapping(value = RESTConstants.SLASH + RESTConstants.IMPORT, method = RequestMethod.POST)
@ResponseBody
public ResponseEntity<List<String>> rolloutImport(@RequestParam(value = "file") List<MultipartFile> files, UriComponentsBuilder builder) {
    HttpHeaders responseHeaders = new HttpHeaders();
    List<String> messages = exportService.rolloutImport(files);
    responseHeaders.setLocation(builder.path(RESTConstants.SLASH + RESTConstants.ROLLOUTS + RESTConstants.SLASH + RESTConstants.IMPORT).queryParam("file", files).buildAndExpand().toUri());
    return new ResponseEntity<List<String>>(messages, responseHeaders, HttpStatus.OK);
}

在控制器类之外:

@Controller
@RequestMapping(RESTConstants.SLASH + RESTConstants.ROLLOUTS)
public class RolloutImportController {

进入任何其他控制器类,比如进入控制器类:

@Controller
@RequestMapping(RESTConstants.SLASH + RESTConstants.ROLLOUTS)
public class RolloutExportController {

让它完美无缺。

我不知道为什么。

我怀疑两个控制器类具有相同的映射:

@RequestMapping(RESTConstants.SLASH + RESTConstants.ROLLOUTS)

可能会遇到麻烦并重新命名其中一个,但它并没有改变任何问题。

然后我怀疑&#34; import&#34;处理程序映射中的单词是问题的一部分,比如,它被映射到一些Spring Security配置或其他东西(源代码搜索没有返回&#34; import&#34;无论如何),所以我也改变了处理程序本身的映射:

@RequestMapping(value = RESTConstants.SLASH + RESTConstants.IMPORT, method = RequestMethod.POST)

为:

@RequestMapping(value = RESTConstants.SLASH + "myimp", method = RequestMethod.POST)

但它也没有改变任何问题。

现在,我对我的工作感到满意,但我仍然对这个问题感到困惑。

0 个答案:

没有答案