2017-04-26 124 views
0

我試圖使用Spring Restful Web服務將數據保存到數據庫。415不支持的媒體類型

我在郵遞員中運行此代碼時不斷得到不受支持的媒體類型錯誤。

我已經編輯我的前端JSP代碼和庫類代碼..

我還添加了pom.xml文件傑克遜依賴。我無法弄清楚我的代碼有什麼問題,因爲我是Restful Web服務的新手。

控制器::

@RequestMapping(value="/insp_rep/{id}",method=RequestMethod.POST, headers = "Accept=application/json") 
    ResponseEntity <Void> addRepo(@RequestBody PC_Repo report, @PathVariable("id") int id){ 
     this.pmService.addRep(report); 
     return new ResponseEntity<Void>(HttpStatus.CREATED); 

    } 

JSP代碼與AngularJS腳本

<html data-ng-app="formSubmit"> 
<head> 

<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js"></script> 

<script type="text/javascript"> 
    var app = angular.module('formSubmit', []); 

    app.controller('FormSubmitController',[ '$scope', '$http', function($scope, $http) { 

     $scope.list = []; 
      $scope.headerText = 'Inspection Report'; 

      $scope.submit = function() { 

       var formData = { 

         "ins_id": $scope.ins_id, 
         "date_insp": $scope.date_insp, 
         "bedrooms":$scope.bedrooms, 
         "balcony":$scope.balcony, 
         "kitchen":$scope.kitchen, 

         "bath_toilet":$scope.bath_toilet, 
         "parking_gar":$scope.parking_gar, 
         "garden":$scope.garden, 
         "others":$scope.others, 
         "action":$scope.action 

       }; 

       var response = $http.post('/PM/insp_rep/{id}', formData); 
       response.success(function(data, status, headers, config) { 
        $scope.list.push(data); 



       }); 
       response.error(function(data, status, headers, config) { 
        alert("Exception details: " + JSON.stringify({data: data})); 

       }); 

       //Empty list data after process 
       $scope.list = []; 

      }; 
     }]); 
</script> 

repository類

@Entity 
@JsonIgnoreProperties({"hibernateLazyInitializer","handler"}) 
@Table(name="abc") 
public class abc implements Serializable { 

    private java.sql.Date date_insp; 
    private String bedrooms; 
    private String balcony; 
    private String kitchen; 
    private String bath_toilet; 
    private String parking_gar; 
    private String garden; 
    private String others; 
     private String title; 
private byte[]photo; 
+0

問題是在你request.It相關看來你是不以正確的JSON format.Check發送請求的依賴就像傑克遜出現在類路徑或沒有。 – Vaibs

+0

嗨Vaibs,謝謝你的答..我已經在pm.xml文件中添加了依賴項。請讓我知道,如果我添加了一個錯誤的依賴......我添加的依賴關係是: \t \t com.fasterxml.jackson.core \t \t 傑克遜,數據綁定 \t \t 2.4.1 \t \t Pra

+0

它似乎correct.check現在 – Vaibs

回答

0

按照沿着上面評價本身解釋ction,obj由所有提交併傳遞給控制器​​的表單參數組成。

JSP HTML標籤:

<input type="text" ng-model="obj.balcony" /> 
<input type"text" ng-model="obj.address" /> 

並在控制器,請嘗試使用警報調試對象價值和轉換成字符串對象使用 'JSON.Stringify(OBJ)'。

控制器JS:

$scope.submit = function() { 
       var formData = $scope.obj; 
       alert(JSON.stringify($scope.obj)); 
       var response = $http.post('/PM/insp_rep/{id}', JSON.Stringify(obj)); 
       response.success(function(data, status, headers, config) { 
        $scope.list.push(data); 
       }); 
       response.error(function(data, status, headers, config) { 
        alert("Exception details: " + JSON.stringify({data: data})); 
       }); 

       //Empty list data after process 
       $scope.list = []; 
      }; 
     }]); 
</script> 
+0

嗨,謝謝你的回覆。我嘗試了上面粘貼的代碼,但我仍然遇到同樣的錯誤。 – Pra

+0

您能否請您使用正確的請求參數從瀏覽器的控制檯添加發布請求的快照。使用開發人員控制檯在瀏覽器中檢查(在瀏覽器中按F12)。我編輯了答案。在發佈請求中傳遞JSON.stringify(obj),並在此處發佈錯誤和快照。 – Vaibs

相關問題