我有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;
}
提前感谢您的帮助。
答案 0 :(得分:2)
您将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模板变量的注释。
答案 1 :(得分:1)
我找到了解决方案:How to make Apache Tomcat accept DELETE method
我添加到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>