【发布时间】:2015-05-08 12:04:36
【问题描述】:
我的 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>
Angular 控制器中的功能
$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;
}
提前感谢您的帮助。
【问题讨论】:
标签: java angularjs spring rest spring-mvc