我想有一个双向JSON到Java序列化
我正在成功使用Java到JSON到JQuery路径......(@ResponseBody)例如
@RequestMapping(value={"/fooBar/{id}"}, method=RequestMethod.GET)
public @ResponseBody FooBar getFooBar(
@PathVariable String id,
HttpServletResponse response , ModelMap model) {
response.setContentType("application/json");
...
}
Run Code Online (Sandbox Code Playgroud)
在我使用的JQuery中
$.getJSON('fooBar/1', function(data) {
//do something
});
Run Code Online (Sandbox Code Playgroud)
这很有效(例如注释工作已经完成,感谢所有的回答者)
但是,如何执行反向路径:使用RequestBody将JSON序列化为Java对象?
无论我尝试什么,我都无法得到这样的东西:
@RequestMapping(value={"/fooBar/save"}, method=RequestMethod.POST)
public String saveFooBar(@RequestBody FooBar fooBar,
HttpServletResponse response , ModelMap model) {
//This method is never called. (it does when I remove the RequestBody...)
}
Run Code Online (Sandbox Code Playgroud)
我已经正确配置了Jackson(它在出路时序列化)并且当然我将MVC设置为注释驱动
我如何使其工作?它有可能吗?或者Spring/JSON/JQuery是单向的(out)?
更新:
我改变了杰克逊的设定
<bean id="jsonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
<!-- Bind the return value of the Rest …Run Code Online (Sandbox Code Playgroud) 我有两个Spring MVC控制器方法.两者都接收请求体中相同的数据(在HTLM的格式POST形式:version=3&name=product1&id=2),但一个方法处理PUT请求和另一DELETE:
@RequestMapping(value = "ajax/products/{id}", method = RequestMethod.PUT)
@ResponseBody
public MyResponse updateProduct(Product product, @PathVariable("id") int productId) {
//...
}
@RequestMapping(value = "ajax/products/{id}", method = RequestMethod.DELETE)
@ResponseBody
public MyResponse updateProduct(Product product, @PathVariable("id") int productId) {
//...
}
Run Code Online (Sandbox Code Playgroud)
在第一种方法中,product参数的所有字段都已正确初始化.在第二个中,仅id初始化字段.其他字段是null或0.(id可能是因为id路径变量而初始化).
我可以看到该HttpServletRequest对象包含请求体(version=3&name=product1&id=2)中所有字段的值.它们只是没有映射到product参数的字段.
如何使第二种方法有效?
我也尝试使用带@RequestParam注释的参数.在处理PUT请求的方法中,它可以工作.在DELETE方法中,我得到一个例外:org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'version' is not present.
我需要在 …
我正在尝试PUT使用jQuery 1.6(Jackson 2.1.1和Spring 3.2.0)通过JSON方法将数据插入和/或更新到数据库中.
JS代码如下.
var itemsArray=[];
var id;
function insertOrUpdate()
{
var i=0;
$('input[name="txtCharge[]"]').each(function()
{
isNaN($(this).val())||$(this).val()==''?itemsArray[i][2]='':itemsArray[i][2]=$(this).val();
i++;
});
$.ajax({
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
datatype:"json",
type: "PUT",
url: "/wagafashion/ajax/InsertZoneCharge.htm",
data: "items=" + JSON.stringify(itemsArray)+"&zoneId="+id+"&t="+new Date().getTime(),
success: function(response)
{
alert(response);
},
error: function(e)
{
alert('Error: ' + e);
}
});
}
Run Code Online (Sandbox Code Playgroud)
Spring控制器内部用URL映射的方法如下.
@RequestMapping(value=("ajax/InsertZoneCharge"), method=RequestMethod.PUT, produces=MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody String insertZoneCharge(@RequestBody final MultiValueMap<String, String > data, final HttpServletResponse response, HttpServletRequest request)
{
String message="";
try
{
Map<String, …Run Code Online (Sandbox Code Playgroud)