我正在运行 Spring 3.1.2 应用程序。我有一个带有多种方法的 RESTful servlet。 GET 方法在 100% 的时间里都能正常工作(@PathVariables 匹配、响应正确编码为基于 Accept header 的 JSON 或 XML 等)。
但是 POST 方法根本不起作用。经过数小时的转换以及我能找到的所有其他 Spring 方面(所有修补都恢复了),我将它缩小到 @RequestParam 中的 required 字段。这是我用来调查的一种简化测试方法:
@RequestMapping (value = "/bogus",
method = POST)
public @ResponseBody PassResponse bogus (
@RequestParam (value = "test", required = false) String test) {
// Just some handy garbage objects that marshal to JSON/XML
UserResponse user = new UserResponse ();
user.setName (test);
AccountDetail detail = new AccountDetail (user,null);
return new PassResponse (detail);
}
required=false:一切正常(接收并解释参数)。完全符合我的预期
required=true:(或未指定,因为这是默认设置)我一直收到消息“MissingServletRequestParameterException:必需的字符串参数'test'不存在”
客户端 View :
required=true
Request URL:http://localhost:8080/internal-project/rest/bogus
Request Method:POST
Status Code:400 Bad Request
Request Headersview source
Accept:application/json
Connection:keep-alive
Content-Length:12
Host:localhost:8080
Request Payload
test=LALALAA
Response Headersview source
Connection:close
Content-Length:971
Content-Type:text/html;charset=utf-8
Date:Wed, 24 Oct 2012 18:41:05 GMT
Server:Apache-Coyote/1.1
required=false
Request URL:http://localhost:8080/internal-project/rest/bogus
Request Method:POST
Status Code:200 OK
Request Headersview source
Accept:application/json
Connection:keep-alive
Content-Length:12
Host:localhost:8080
Request Payload
test=LALALAA
Response Headersview source
Content-Type:application/json;charset=UTF-8
Date:Wed, 24 Oct 2012 18:44:03 GMT
Server:Apache-Coyote/1.1
Transfer-Encoding:chunked
这与切换 required 时运行的测试套件完全相同,我可以看到参数正在传递。当参数为可选时,Spring 会正确处理。
如果有人以前遇到过这个问题或有任何想法,我很想听听他们的意见。将所需参数标记为可选,即使它有效,即使我评论它也是可怕的 self 文档。再加上这种行为让我有点紧张。希望我只是在某个地方搞砸了……
最佳答案
我认为您的 Content-Type header 应该是 application/x-www-form-urlencoded。
关于Spring不接受POST参数,除非@RequestParam "required=false",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13055885/
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby中使用两个参数异步运行exe吗?我已经尝试过ruby命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何rubygems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除
这是在Ruby中设置默认值的常用方法:classQuietByDefaultdefinitialize(opts={})@verbose=opts[:verbose]endend这是一个容易落入的陷阱:classVerboseNoMatterWhatdefinitialize(opts={})@verbose=opts[:verbose]||trueendend正确的做法是:classVerboseByDefaultdefinitialize(opts={})@verbose=opts.include?(:verbose)?opts[:verbose]:trueendend编写Verb
我有一些Ruby代码,如下所示:Something.createdo|x|x.foo=barend我想编写一个测试,它使用double代替block参数x,这样我就可以调用:x_double.should_receive(:foo).with("whatever").这可能吗? 最佳答案 specify'something'dox=doublex.should_receive(:foo=).with("whatever")Something.should_receive(:create).and_yield(x)#callthere
是的,我知道最好使用webmock,但我想知道如何在RSpec中模拟此方法:defmethod_to_testurl=URI.parseurireq=Net::HTTP::Post.newurl.pathres=Net::HTTP.start(url.host,url.port)do|http|http.requestreq,foo:1endresend这是RSpec:let(:uri){'http://example.com'}specify'HTTPcall'dohttp=mock:httpNet::HTTP.stub!(:start).and_yieldhttphttp.shou
我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby1.9+ 关于ruby-主要:Objectwhenrun
我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳
我知道您通常应该在Rails中使用新建/创建和编辑/更新之间的链接,但我有一个情况需要其他东西。无论如何我可以实现同样的连接吗?我有一个模型表单,我希望它发布数据(类似于新View如何发布到创建操作)。这是我的表格prohibitedthisjobfrombeingsaved: 最佳答案 使用:url选项。=form_for@job,:url=>company_path,:html=>{:method=>:post/:put} 关于ruby-on-rails-rails:Howtomak