2016-06-02 9 views
3

Ich rufe einen Spring Boot REST Service von meinem eckigen UI. Es funktionierte gut, solange der Spring Boot Rest Service als Spring Boot App ausgeführt wurde. Aber sobald ich es in eine WAR-Datei konvertiert und auf einem Jboss 6.2.4-Server implementiert habe, bekomme ich 404. Ich sehe, dass der REST-Service-Aufruf von der UI erfolgreich ist, aber die Anfrage JSON nicht übergeben wird. Auf der Anfrage JSON übergebe ich 2 Strings und eine hochgeladene Excel-Datei.Angular http und Spring Boot Rest Service

Dies ist mein Winkel UI http Aufruf

App.service('getHeatMapDataService', ['$http', '$q', function ($http,  $q) { 
this.getHeatMapData = function (scope) { 
    var url = 'http://localhost:8080/rest-services/fusiontables/upload'; 
    var deferred = $q.defer(); 
    $http({ 
      method: 'POST', 
      url: url, 
      headers: { 
       'Content-Type': undefined 
      }, 
      data: { 
       stateCd: scope.stateCd, 
       addressExtras: scope.addressExtras, 
       uploadFile: scope.upFile 
      }, 
      transformRequest: function (data, headersGetter) { 
       var formData = new FormData(); 
       angular.forEach(data, function (value, key) { 
        formData.append(key, value); 
       }); 

       var headers = headersGetter(); 
       delete headers['Content-Type']; 
       return formData; 
      } 
     }) 
     .success(function (data) { 
      deferred.resolve(data); 
      console.log("Success"); 
      console.log(data); 
     }) 
     .error(function (data, status) { 
      deferred.reject(status); 
      console.log("Failed"); 
     }); 
    return deferred.promise; 
} 
}]); 

Das ist mein Frühling Boot Rast Controller, wenn es funktioniert

@RequestMapping(value="/upload", method=RequestMethod.POST) 
@ResponseBody 
public String getBoundaries(HeatMapUploadCommand uploadCommand) { 
    logger.info("Heat Map Controller invoked " + uploadCommand); 
    return null; 
} 

Dies ist mein Upload-Befehl

public class HeatMapUploadCommand { 

private String stateCd; 
private String addressExtras; 
private MultipartFile uploadFile; 

Nach i Auf Jboss implementiert, trifft die Anfrage immer noch die Spring Boot App, aber dann hat sie alle Anfrageparameter als null.

Dies ist die Anfrage-Payload

------WebKitFormBoundaryvCCnl3nhIgoW1MwR 
Content-Disposition: form-data; name="stateCd" 

CA 
------WebKitFormBoundaryvCCnl3nhIgoW1MwR 
Content-Disposition: form-data; name="addressExtras" 

1234 
------WebKitFormBoundaryvCCnl3nhIgoW1MwR 
Content-Disposition: form-data; name="uploadFile"; filename="CAdata.xlsx" 
Content-Type: application/vnd.openxmlformats- officedocument.spreadsheetml.sheet 


------WebKitFormBoundaryvCCnl3nhIgoW1MwR-- 

ich als

Änderung der Controller versucht
@RequestMapping(value="/upload", method=RequestMethod.POST) 
@ResponseBody 
public String getBoundaries(@RequestParam(value="stateCd") String stateCd, 
     @RequestParam(value="addressExtras") String addressExtras, 
     @RequestParam(value="uploadFile") MultipartFile file) { 
    System.out.println("Heat Map Controller invoked " + stateCd); 
    return null; 
} 

Noch kein Glück. Das ist die Antwort, die ich bekommen habe.

{"timestamp":1464840821648,"status":400,"error":"Bad Request","exception":"org.springframework.web.bind.MissingServletRequestParameterException","message":"Required String parameter 'stateCd' is not  present","path":"/rest-services/fusiontables/upload"} 

Antwort

0

dachte es schließlich heraus.

Ich musste eine Multipartresolver Bean hinzufügen.

@Configuration 
public class WebConfig extends WebMvcAutoConfiguration { 
@Bean 
public MultipartResolver multipartResolver() { 
    return new CommonsMultipartResolver(); 
} 

}

, dass das Problem behoben. Scheint, als wenn ich es als eine Spring-Boot-App ausführen, kümmert sich Frühling schon, aber wenn als WAR-Datei bereitgestellt sollte diese Bean konfiguriert werden

3

Ich glaube, Sie haben @RequestBody verpasst. Wenn Sie $http mit Dateneigenschaft verwenden, werden die Daten in request body übergeben.

$http({ 
    method: 'POST', 
    url: url, 
    data: { 
     stateCd: scope.stateCd, 
     addressExtras: scope.addressExtras, 
     uploadFile: scope.upFile 
    }, 

Ich denke, wenn Sie @RequestBody hinzufügen, kann es funktionieren.

@RequestMapping(value="/upload", method=RequestMethod.POST) 
@ResponseBody 
public String getBoundaries(@RequestBody HeatMapUploadCommand uploadCommand) { 
    logger.info("Heat Map Controller invoked " + uploadCommand); 
    return null; 
} 
+0

Jetzt bekomme ich HTTP-Status 415 nicht unterstützten Medientyp. {"timestamp": 1464874683832, "status": 415, "error": "Nicht unterstützter Medientyp", "Ausnahme": "org.springframework.web.HttpMediaTypeNotSupportedException", "message": "Inhaltstyp" multipart/form-data ; boundary = ---- WebKitFormBoundarymRKqwcUfa6CJzALs; charset = UTF-8 'nicht unterstützt "," Pfad ":"/rest-services/fusiontables/upload "} –

0

können Sie versuchen, mit diesem Feder Controller-Methode:

@RequestMapping(value="/upload", method=RequestMethod.POST) 
@ResponseBody 
public String getBoundaries(MultipartHttpServletRequest request, HttpServletResponse response) throws IOException { 
    String stateCd = request.getParameter("stateCd"); 
    String addressExtras = request.getParameter("addressExtras"); 
    ...... 
    Iterator<String> iterator = request.getFileNames(); 
    while (iterator.hasNext()) { 
      MultipartFile multipartFile = request.getFile(iterator.next()); 
      ..... 
    } 
    ......... 
} 

und der Winkel Service:

App.service('getHeatMapDataService', ['$http', '$q', function ($http,$q) { 
    this.getHeatMapData = function (scope) { 
    var url = 'http://localhost:8080/rest-services/fusiontables/upload'; 
    var deferred = $q.defer(); 
    var formData = new FormData(); 
    formData.append('stateCd', scope.stateCd); 
    formData.append('addressExtras', scope.stateCd); 
    formData.append('file', scope.upFile); 

    $http.post(url, formData, { 
     headers: { 'Content-Type': undefined }, 
     transformRequest: angular.identity 
    }).success(function (result) { 
     console.log('200'); 
    }).error(function() { 
     console.log('400-500'); 
    }); 
    return deferred.promise; 
    } 
}]); 
+0

Das hat nicht so gut funktioniert. Ich bekomme immer noch Nullen auf allen Feldern. –

+0

Welche Felder erhalten Sie null? Wie sieht es in der Anfrage-Payload aus? –

+0

Ich habe die Antwort mit mod anguliert Service aktualisiert. –