嗨,我在Spring中开始使用Web Services,所以我正在尝试在Spring + JSON + Hibernate中开发小型应用程序.我在HTTP-POST方面遇到了一些问题.我创建了一个方法:
@RequestMapping(value="/workers/addNewWorker", method = RequestMethod.POST, produces = "application/json", consumes = "application/json")
@ResponseBody
public String addNewWorker(@RequestBody Test test) throws Exception {
String name = test.name;
return name;
}
Run Code Online (Sandbox Code Playgroud)
我的模型Test看起来像:
public class Test implements Serializable {
private static final long serialVersionUID = -1764970284520387975L;
public String name;
public Test() {
}
}
Run Code Online (Sandbox Code Playgroud)
通过POSTMAN,我只发送JSON {"name":"testName"},我总是得到错误;
The server refused this request because the request entity is in a format not supported by the requested resource for the requested method.
Run Code Online (Sandbox Code Playgroud)
我进口杰克逊库.我的GET方法运行正常.我不知道我做错了什么.我很感激任何建议.
我想从rake任务调用控制器动作.我的问题是什么方法最好准备http请求?感谢所有提示.
编辑:有人提示吗?我试过这个而不是工作:
controller_obj = Controller.new
controller.your_method
Run Code Online (Sandbox Code Playgroud)
我有这个例外:
rake aborted!
uninitialized constant Controller
Run Code Online (Sandbox Code Playgroud)
Edit2:我试过了:
sess = ActionController::Integration::Session.new
sess.post('/route', 'codes=3')
Run Code Online (Sandbox Code Playgroud)
但我得到了(我在rake文件中要求'action_controller/integration'):
rake aborted!
cannot load such file -- action_controller/integration
Run Code Online (Sandbox Code Playgroud) 我正在学习 rails rspec 中的测试。我有这部分测试:
subject(:service) { described_class.new() }
context 'when sth' do
it 'should sth' do
expect ( service.call(@params) ).to eq(0)
end
end
Run Code Online (Sandbox Code Playgroud)
服务的方法调用返回编号。但我得到这样的东西:
失败:
1) Module::Registration when sth should sth
Failure/Error: expect ( service.call(@params) ).to eq(2)
NoMethodError:
undefined method `to' for 2:Fixnum
# ./spec/module/registration_spec.rb:22:in `block (3 levels) in <module:Module>'
Finished in 2.48 seconds
1 example, 1 failure
Run Code Online (Sandbox Code Playgroud)
那么如何检查该方法使用 to 返回正确的“Number”变量?
如何存根:包含模块的超级方法.我有以下控制器:
class ImportsController < BaseController
include ImportBackend
def import_from_file
super
rescue TransferPreview::Error => exc
flash[:error] = "Some String"
redirect_to imports_path
end
end
Run Code Online (Sandbox Code Playgroud)
和importBackend模块:
module ImportBackend
def import_from_file
//something
end
end
Run Code Online (Sandbox Code Playgroud)
我想测试那个控制器.我的问题是如何在ImportBackend中存根方法来引发错误?我试过几个解决方案但没有任何作用
ImportBackend.stub(:import_from_file).and_raise(Transfer::Error)
controller.stub(:super).and_raise(Transfer::Error)
controller.stub(:import_from_file).and_raise(Transfer::Error)
Run Code Online (Sandbox Code Playgroud)
谢谢你的所有答案.
我有以下情况:
<div class="control-group">
<label class="control-label" for="first_name">
First name
</label>
<div class="controls">
<input id="first_name" type="text" value="rybka">
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
我有输入的ID.我想使用"control-group"类向div添加class,并使用指定的id输入.如果做到这一点的完美方式怎么办?
编辑:
感谢您使用该解决方案的所有答案:
$(".control-group:has(#specificId)").addClass('yourClass');
Run Code Online (Sandbox Code Playgroud) 我决定在ruby中创建我的第一个宝石,我遇到了问题.如果用户将我的gem添加到他们的gemfile中,我想在数据库中创建一个新表.
我相信唯一的解决方案是创建一个rake任务,该任务通过迁移运行自定义脚本.我对吗?你知道有关使用普通Ruby修改数据库的任何教程吗?如何创建此脚本?还有其他解决方案吗?