Spring REST Controller没有响应Angular请求

use*_*256 5 spring-mvc spring-boot spring-restcontroller

我有一个应用程序来创建服务器证书请求,就像一个人使用java keytool或其他东西.我正在尝试将创建的证书请求和密钥返回到zip文件中,但对于我的生活,我无法让我的REST控制器响应http请求.更正:控制器响应,但方法中的代码永远不会执行.

服务器确实收到请求,因为我的CORS过滤器已执行.但是我在控制器方法中有一个调试集,它永远不会触发.方法的签名是否正确?我需要另一双眼睛吗?

这是我的控制器代码:

@RequestMapping(method = RequestMethod.POST, value = "/generateCert/")
public ResponseEntity<InputStreamResource> generateCert(@RequestBody CertInfo certInfo) {
    System.out.println("Received request to generate CSR...");

    byte[] responseBytes = commonDataService.generateCsr(certInfo);
    InputStreamResource resource = new InputStreamResource(new ByteArrayInputStream(responseBytes));

    System.out.println("Generated CSR with length of " + responseBytes.length);
    return ResponseEntity.ok()
            .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=certificate.zip")
            .contentType(MediaType.parseMediaType("application/zip"))
            .contentLength(responseBytes.length)
            .body(resource);
}
Run Code Online (Sandbox Code Playgroud)

这是Angular请求:

generateCertificate(reqBody: GenerateCert) {
   let headers = new Headers();
   headers.append('Content-Type', 'application/json');

   this.http.post(this.urlGenerateCert, JSON.stringify(reqBody), {headers: headers}).subscribe(
    (data) => {
        let dataType = data.type;
        let binaryData = [];
        binaryData.push(data);
        this.certBlob = new Blob(binaryData);
    });
    return this.certBlob;
 }
Run Code Online (Sandbox Code Playgroud)

最后,我从网络面板复制了请求和响应标头:

Response
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: Content-Type, Authorization, Accept, X-Requested-With, remember-me
Access-Control-Allow-Methods: POST, GET, OPTIONS
Access-Control-Allow-Origin: *
Access-Control-Max-Age: 3600
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Content-Length: 0
Date: Thu, 27 Dec 2018 22:48:00 GMT
Expires: 0
Location: http://localhost:8102/login
Pragma: no-cache
Set-Cookie: JSESSIONID=EDACE17328628D579670AD0FB53A6F35; Path=/; HttpOnly
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block

Request
Accept: application/json, text/plain, */*
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Connection: keep-alive
Content-Length: 205
Content-Type: application/json
Host: localhost:8102
Origin: http://localhost:4200
Referer: http://localhost:4200/generateCerts
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36
Run Code Online (Sandbox Code Playgroud)

我真的很难让CORS工作,所以也许这会干扰请求?除非绝对必要,否则我讨厌发布所有代码.有人有任何想法吗?

use*_*256 0

感谢所有做出贡献的人。我发现该错误是由于我的控制器方法的标头造成的。更改后,该方法被正确调用。这是有效的:

@RequestMapping(method = RequestMethod.POST, path = "/generateCert", 
    produces = {MediaType.APPLICATION_OCTET_STREAM_VALUE}, consumes = {MediaType.APPLICATION_JSON_VALUE})
public ResponseEntity<byte[]> generateCert(@RequestBody CertInfo certInfo) {
    byte[] responseBytes = commonDataService.generateCsr(certInfo);    
    return ResponseEntity.ok()
            .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_VALUE)
            .contentLength(responseBytes.length)
            .body(responseBytes);
}
Run Code Online (Sandbox Code Playgroud)