415不支持的媒体类型

时间:2017-04-26 10:15:43

标签: rest spring-mvc postman spring-restcontroller

我正在尝试使用Spring Restful Web服务将数据保存到数据库。

在Postman中运行此代码时,我不断收到不支持的媒体类型错误。

我用前端JSP代码和Repository类编辑了我的代码..

我还在pom.xml文件中添加了jackson依赖项。由于我是Restful网络服务的新手,我无法弄清楚我的代码有什么问题。

Controller ::

@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>

存储库类

@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;

1 个答案:

答案 0 :(得分:0)

如上面评论部分所述,obj包含提交并传递给控制器​​的所有表单参数。

JSP HTML标记:

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

在控制器中,尝试使用alert调试对象值并使用String转换对象&#39; JSON.Stringify(obj)&#39;。

控制器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>