簡體   English   中英

使用Spring MVC和AngularJS的方法DELETE

[英]Method DELETE with Spring MVC and AngularJS

我有RESTfull服務的問題,它應該從數據庫中刪除記錄。 當我使用REST請求調用角度函數時,我在FireBug中收到錯誤消息:“NetworkError:403 Forbidden - http:// localhost:8080 / sake / dict / technologies ”。 問題只有DELETE方法 - GET,POST工作正常。

Angular服務

(function(angular) {
'use strict';
angular.module('dictionaryService', ['ngResource'])
    .factory('dictTechnologies', [ '$resource', function($resource) {
        return $resource('http://localhost:8080/sake/dict/technologies/:id', {id: '@id' }, {
            query: {method:'GET', isArray:true},
            create: {method:'POST'},
            delete: {method:'DELETE', params: {id: '@id'}}
        });
    }]).factory('dictDocuments', [ '$resource', function($resource) {
        return $resource('http://localhost:8080/sake/dict/documents/:Id', {Id: "@Id" }, {
            query: {method:'GET', isArray:true},
            create: {method:'POST'},
            delete: {method:'DELETE'}
        });
    }]);
})(window.angular);

HTML我按下按鈕

 <button ng-click="deleteBtn()" class="btn btn-primary btn-xs"> Delete [x] </button>

角度控制器中的功能

$scope.deleteBtn= function() {
        var z = $scope.technologies[1].id; //here is JSON of technologu which I recieved in GET response
        console.log(z);
        dictTechnologies.delete(z).$promise.then(function(z) {
            //if success
        }, function(errResoponse) {

        });
    };

Java Spring MVC中的控制器使示例更容易我只需調用System.out.println();

@Controller
@RequestMapping("/dict")
public class SlownikController {


    @Autowired
    SlTechnologiaDao slTechnologiaDao;

//GET work fine
        @RequestMapping(value = "/technologies", method = RequestMethod.GET)
        public @ResponseBody List<SlTechnologia> getAllTechnologies() {
 return slTechnologiaDao.getAllTechnologia();
        }

    @RequestMapping(value = "/technologies/{id}", method = RequestMethod.DELETE)
    public @ResponseBody int deleteTechnology(@RequestParam("id")/* @PathParam("id")*/ Integer id) {
        System.out.println(id);
        return 1;
    }

}

AplicationContext - servlet

<!-- Configure to plugin JSON as request and response in method handler -->
    <bean
        class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="jsonMessageConverter" />
            </list>
        </property>
    </bean>

    <!-- Configure bean to convert JSON to POJO and vice versa -->
    <bean id="jsonMessageConverter"
        class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
    </bean>

我讀了很多頁,到處都是方法相同:

@RequestMapping(value = "/technologies/{id}", method = RequestMethod.DELETE)
        public @ResponseBody int deleteTechnology(@RequestParam("id")/* @PathParam("id")*/ Integer id) {
            System.out.println(id);
            return 1;
        }

提前感謝您的幫助。

您將id作為路徑變量傳遞,但嘗試將其作為請求參數。 將您的方法更改為

 @RequestMapping(value = "/technologies/{id}", method = RequestMethod.DELETE)
 public @ResponseBody int deleteTechnology(@PathVariable("id") Integer id) {
        System.out.println(id);
        return 1;
 }

@RequestParam是一個注釋,表示方法參數應綁定到Web請求參數,而@PathVariable是指示方法參數應綁定到URI模板變量的注釋。

我找到了解決方案: 如何使Apache Tomcat接受DELETE方法

我添加到web.xml過濾器和DELETE工作:

<filter>
        <filter-name>CorsFilter</filter-name>
        <filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
        <init-param>
            <param-name>cors.allowed.headers</param-name>
            <param-value>Accept,Accept-Encoding,Accept-Language,Access-Control-Request-Method,Access-Control-Request-Headers,Authorization,Connection,Content-Type,Host,Origin,Referer,Token-Id,User-Agent, X-Requested-With</param-value>
        </init-param>
        <init-param>
            <param-name>cors.allowed.origins</param-name>
            <param-value>*</param-value>
        </init-param>
        <init-param>
            <param-name>cors.allowed.methods</param-name>
            <param-value>GET, POST, PUT, DELETE, OPTIONS, HEAD</param-value>
        </init-param>
    </filter>
<filter-mapping>
    <filter-name>CorsFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM