Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 请求方法';删除';春季不受支持_Angularjs_Spring - Fatal编程技术网

Angularjs 请求方法';删除';春季不受支持

Angularjs 请求方法';删除';春季不受支持,angularjs,spring,Angularjs,Spring,我正在尝试通过angular服务访问rest方法。下面是我的rest方法代码 @RestController @RequestMapping("/api") public class UserController { @RequestMapping(value ="/removeUserRole/{roleId}", method = RequestMethod.DELETE) public void removeUserRole(@PathVariable("roleId")L

我正在尝试通过angular服务访问rest方法。下面是我的rest方法代码

@RestController
@RequestMapping("/api")
public class UserController { 

  @RequestMapping(value ="/removeUserRole/{roleId}", method = RequestMethod.DELETE)
    public void removeUserRole(@PathVariable("roleId")Long roleId){
        System.out.println("inside delete method");
    }

}
这是我的angularJs服务方法

App.factory('manageRoleService', ['$resource',
    function($resource) {
        return {
            getUserRoleDetails: $resource('api/getUserRoleDetails', {}, {
                query: {method: 'GET', isArray: true}
            }),
            removeUserRole: $resource('api/removeUserRole/:roleId', {}, {
                delete: {method: 'DELETE', params: {id: '@roleId'}}
            })
        };
    }]);
get方法工作得很好。但当我尝试调用delete方法时,它会给出以下错误

405方法不允许

不支持请求方法“删除”

我做错了什么。我试着在同一个问题上遵循一些线索,但没有任何帮助


提前谢谢

删除
url模板中,您正在使用
:roleId
占位符,但稍后提供
id
变量名

由于最后一个
/
后面的零件为空,因此弹簧无法找到正确的映射

改变

 {id: '@roleId'}
致:


假设
@roleId
引用了您所拥有的数据对象的属性-也许它应该是
@id

我通过玩了一会儿就解决了这个问题。我猜这是url模式之间的不匹配

这是我的新代码

@RestController
@RequestMapping("/api/role")
public class RoleController {

    private static final Logger logger = LoggerFactory.getLogger(RoleController.class);

    @Autowired
    private RoleService roleService;

    @RequestMapping(method = RequestMethod.GET)
    public List<ModulePermission> getAllRoles(){
        return roleService.getModulePermissions();
    }

    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    public void remove(@PathParam("id") Long id){
       logger.info("Deleting the role with id:" + id);
       roleService.deleteRole(id);
    }
}

App.factory('manageRoleService', ['$resource',
    function($resource) {
        return {
            getUserRoleDetails: $resource('api/role', {}, {
                query: {method: 'GET', isArray: true}
            }),
            removeUserRole: $resource('api/role/:id', {}, {
                delete: {method: 'DELETE', params: {id: '@id'}}
            })
        };
    }]);
@RestController
@请求映射(“/api/role”)
公共类角色控制器{
私有静态最终记录器Logger=LoggerFactory.getLogger(RoleController.class);
@自动连线
私人角色服务角色服务;
@RequestMapping(method=RequestMethod.GET)
公共列表getAllRoles(){
返回roleService.getModulePermissions();
}
@RequestMapping(value=“/{id}”,method=RequestMethod.DELETE)
公共无效删除(@PathParam(“id”)长id){
logger.info(“删除id为:+id的角色”);
roleService.deleteRole(id);
}
}
App.factory('manageRoleService',['$resource',
职能(资源){
返回{
getUserRoleDetails:$resource('api/role',{}{
查询:{method'GET',isArray:true}
}),
removeUserRole:$resource('api/role/:id',{}{
删除:{method:'delete',参数:{id:'@id'}
})
};
}]);

但是现在,当我调试rest方法时,似乎没有传递该值。它打印为空

请显示服务器对此事的跟踪。如果您打开MVC的信息日志记录级别并在此处显示日志,这将非常棒。这是服务器日志15:20:01603 WARN[org.springframework.web.servlet.PageNotFound](默认任务-27)中打印的内容请求方法“DELETE”不受支持您可能希望将org.springframework.web.servlet的日志级别设置为调试甚至跟踪,并查看是否打印了任何奇怪的映射。您好,我尝试按您所述更改它,但收到相同的错误。然后我继续将它的参数名更改为id,但仍然得到相同的错误。removeUserRole:$resource('api/removeUserRole/:id',{},{delete:{method:'delete',params:{id:'@id'}}}}})@ChathruakaWaas GET处理程序在哪里?在同一个班?请把它寄出去。另外,请查看您的浏览器控制台并检查删除请求发送到的url。另一个选项可能是使用curl测试它。通过使用pathVariable而不是pathParam来修复它。谢谢你的建议。
@RestController
@RequestMapping("/api/role")
public class RoleController {

    private static final Logger logger = LoggerFactory.getLogger(RoleController.class);

    @Autowired
    private RoleService roleService;

    @RequestMapping(method = RequestMethod.GET)
    public List<ModulePermission> getAllRoles(){
        return roleService.getModulePermissions();
    }

    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    public void remove(@PathParam("id") Long id){
       logger.info("Deleting the role with id:" + id);
       roleService.deleteRole(id);
    }
}

App.factory('manageRoleService', ['$resource',
    function($resource) {
        return {
            getUserRoleDetails: $resource('api/role', {}, {
                query: {method: 'GET', isArray: true}
            }),
            removeUserRole: $resource('api/role/:id', {}, {
                delete: {method: 'DELETE', params: {id: '@id'}}
            })
        };
    }]);