首页 > 解决方案 > Spring,Angular.js 415 不支持的媒体类型错误

问题描述

这个简单的请求遇到了 415 Unsupported Media Type 错误。

http://localhost:8080/semantic/api/getLastfmGraph/1

这是我的控制器代码:

@RequestMapping(value = "/getLastfmGraph/{userId}", method = RequestMethod.GET, consumes = MediaType.APPLICATION_JSON_VALUE,
            produces = MediaType.APPLICATION_JSON_VALUE)
    public
    @ResponseBody
    Object getLastfmGraph(@PathVariable Long userId) throws Exception {
        User user = semanticGraphDao.findUserById(userId);

        List<Node> nodes = lastfm.topArtistsByUser(user.getLastfmUsername());
        List<Link> links = new ArrayList<>();
        for (Node n : nodes) {
            for (Node n2 : nodes) {
                if(!StringUtils.equals(n.getName(),n2.getName()))
                    links.add(new Link(n, n2, 1.0));
            }
        }

        return new Graph(nodes,links);
    }

标签: angularspringresthttp-status-code-415

解决方案


我通过删除 consumes='application/json' 解决了这个问题。


推荐阅读