Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.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
Spring REST方法不会放置或发布到服务器_Spring_Rest_Spring Mvc - Fatal编程技术网

Spring REST方法不会放置或发布到服务器

Spring REST方法不会放置或发布到服务器,spring,rest,spring-mvc,Spring,Rest,Spring Mvc,我试图在我的Spring应用程序中使用一些REST方法,但似乎没有成功。我显然错过了一些东西,但我不能告诉我的生活会是什么。这是我的控制器: @Controller public class IndexController { static Logger log = Logger.getLogger(IndexController.class); @Autowired private ProvisionService provisionService; @Re

我试图在我的Spring应用程序中使用一些REST方法,但似乎没有成功。我显然错过了一些东西,但我不能告诉我的生活会是什么。这是我的控制器:

@Controller
public class IndexController {
    static Logger log = Logger.getLogger(IndexController.class);

    @Autowired
    private ProvisionService provisionService;

    @RequestMapping(value="/home/data", method=RequestMethod.GET,
                    headers="Accept=application/json")
    public @ResponseBody List<Provision> getData() {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String username = null;

        if(principal instanceof UserDetails)
            username = ((UserDetails)principal).getUsername();

        return provisionService.getAllByUser(username);
    }

    //JSON put request - doesn't work currently
    @RequestMapping(value="/home/data", method=RequestMethod.PUT,
                    headers="Content-Type=application/json")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void updateProvisions(@RequestBody List<Provision> provisions) {
        log.info("Provisions: " + provisions.toString());
    }

    @RequestMapping(value={"/","/home"}, method=RequestMethod.GET)
    public void showIndex() {}
}

使用POST提交表单。您得到一个不支持的帖子错误


在上面,我看到您正在使用RequestMethod.PUT输入源代码。这里根本没有提到POST。

添加您需要添加一个参数\u方法,并将值放入您的请求中。而不是json内容

因此,在第一步中,我将请求的URL更改为/home/data?\u method=PUT


如果这项工作成功,您可以搜索如何在不干扰Json数据的情况下将_方法参数添加到请求内容中。

您使用标题更新了您的问题,是否可以将整个请求的实际转储值放在那里,以查看发送的_方法参数

另外,虽然我认为headers=-规则是有效的,但它们不应该是必需的。您有一个json转换器bean,它将根据内容类型和接受头进行marshall和unmarshall处理,如果找不到有效的转换器,Spring将返回一个错误

将它包含在@RequestMapping中的唯一原因是,如果您使用xml而不是json调用一个方法,那么该方法实际上做了其他事情,这听起来像是一个糟糕的设计


删除这些标题规则并重试,使其尽可能简单并逐渐添加逻辑。

>然而,当我尝试使用此表单返回Json时,我得到了一个405。。。您发送的请求的外观如何。如果删除headers=Content-Type=application/json约束,是否有效?@Ralph No,结果相同。此外,这不是必须的,这样它就知道它正在获取Json对象并可以对其进行解码吗?对/home/data的Json请求。它是否确实包含用于更改方法HiddenHttpMethodFilter的参数?请使用一些工具检查这一点,或者在HiddenHttpMethodFilter中设置一个bikPoint。当我检查JSP的源代码时,它确实使用适当的值创建了一个隐藏的输入。所以是的,我想是的。然而,我不会在服务器端重新使用该方法,以便能够对其进行进一步的评估。这是Spring为您所做的工作的一部分。因为我使用的是HiddenHttpMethodFilter和Spring表单标记,所以它为我处理该部分。表单使用POST呈现,隐藏值为PUT,Spring会为您翻译它。我不需要一个找工作的人。不管怎样,我想到了这一点,并尝试将我的RequestMethod更改为POST,但我得到了完全相同的结果。我不知道这一点。非常感谢。不,那没什么用。我很确定,由于Spring和HiddenHttpMethodFilter,请求中已经有了\u方法。但我试了一下,发现了完全相同的问题。谢谢你的想法。我觉得奇怪的是,我没有以某种方式发回Json对象,但在所有示例中,我都没有看到它们比我做的更多。@cardician:可能您有几个问题,但是如果您不提交_method参数,HiddenHttpMethodFilter就不能做任何事情
<sf:form id="homeForm" method="put" action="${homeData_url}"></sf:form>
http://localhost:8080/NFI/home

POST /NFI/home HTTP/1.1
Host: localhost:8080
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:7.0.1) Gecko/20100101 Firefox/7.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
DNT: 1
Connection: keep-alive
Referer: http://localhost:8080/NFI/home
Cookie: JSESSIONID=584AC21ADE4F214904B9E7E2370363EF
Content-Type: application/x-www-form-urlencoded
Content-Length: 11

HTTP/1.1 405 Method Not Allowed
Server: Apache-Coyote/1.1
Allow: GET, PUT
Content-Type: text/html;charset=utf-8
Content-Length: 1085
Date: Fri, 21 Oct 2011 15:39:26 GMT