使用Spring MVC 3.0生产/使用对称JSON [英] Producing / Consuming Symmetrical JSON with Spring MVC 3.0

查看:98
本文介绍了使用Spring MVC 3.0生产/使用对称JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过Spring配置具有各种表示形式的RESTful Web服务,包括JSON.我希望接口是对称的,这意味着通过GET序列化为JSON的对象的格式也是POST/PUT可以接受的格式.不幸的是,我只能让GETs正常工作.

I am configuring a RESTful web service via Spring, with various representations, including JSON. I want the interface to be symmetrical, meaning the format of an object serialized to JSON via a GET is also the format that a POST/PUT would accept. Unfortunately I can only get GETs to work.

这是我用于发送和接收JSON的配置,其中包含JSON消息转换器和视图:

Here's my configuration for sending and receiving JSON, which consists of a JSON message converter and view:

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
        <util:list>
            <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
        </util:list>
    </property>
</bean>

<bean id="contentNegotiatingViewResolver" class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <property name="mediaTypes">
        <util:map>
            <entry key="json" value="application/json"/>
        </util:map>
    </property>
    <property name="defaultViews">
        <util:list>
            <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"/>
        </util:list>
    </property>
</bean>

当我使用GET击打控制器以返回对象(例如Book)时,它会输出类似的内容.

When I hit a controller with a GET to return an object, for example, a Book, it outputs something like this.

{"book":{"isbn":"1234","author":"Leo Tolstoy","title":"War and Peace"}}

如果我转过头来通过POST或PUT重新提交一些类似的JSON,Spring将无法使用它,并抱怨Unrecognized field "book" (Class com.mycompany.Book), not marked as ignorable.另外,如果我剥离"book"包装器元素(我宁愿不这样做,但只是为了看看会发生什么),我也会得到400错误的请求.无论哪种情况,我的控制器代码都不会被命中.

If I turn around and re-submit some similar JSON via a POST or PUT, Spring cannot consume it, complaining about Unrecognized field "book" (Class com.mycompany.Book), not marked as ignorable. Additionally, if I strip off the "book" wrapper element (I'd rather not, but just to see what happens), I get a 400 BAD REQUEST. In either case, my controller code is never hit.

这是我的控制器-我宁愿这里没有任何特定于JSON的代码(或将我的类的注解编组/解组),因为它们将具有多种表示形式-我想使用Spring的解耦MVC基础架构来推送此类东西(编组/视图解析等)到配置文件中:

Here's my controller - I'd rather not have any JSON-specific code here (or annotations on my classes being marshalled/unmarshalled) as they will have multiple representations - I want to use Spring's decoupled MVC infrastructure that pushes that kind of thing (marshalling/view resolving/etc.) into configuration files:

@RequestMapping(method=PUT, value="/books/{isbn}")
@ResponseStatus(NO_CONTENT)
public void saveBook(@RequestBody Book book, @PathVariable String isbn) {
    book.setIsbn(isbn);
    bookService.saveBook(book)
}

@RequestMapping(method=GET, value="/books/{isbn}")
public ModelAndView getBook(@PathVariable String isbn) {
    return new ModelAndView("books/show", "book", bookService.getBook(isbn));
}

推荐答案

尽管令人尴尬,但我在回答自己的后代问题:-)

Even though it is embarrassing, I am answering my own question for posterity :-)

事实证明,我发布的此示例方法所代表的真实代码中的等效控制器方法:

It turns out that the equivalent controller method in my real code represented by this example method that I posted:

void saveBook(@RequestBody Book book, @PathVariable String isbn)

实际上看起来更像这样(请注意:LongString):

Actually looks more like this (note: Long vice String):

void saveBook(@RequestBody Book book, @PathVariable Long isbn)

并且传递的值不能转换为Long(字母数字).所以...我搞砸了! :-)

And the value being passed can't be converted to a Long (it is alphanumeric). So... I screwed up! :-)

但是,Spring对此并不太好,只是吐出400 Bad Request.我连接了调试器以发现这一点.

However, Spring wasn't very nice about it and simply spit out 400 Bad Request. I attached a debugger to discover this.

使用ModelAndView仍会生成一个外部包装器元素,我将不得不以某种方式处理该元素(因为我想使用ModelAndView来支持JSP视图等).我可能必须为此提供自定义视图.

The use of ModelAndView still generates an outer wrapper element that I will have to deal with somehow (as I want to user ModelAndView to support JSP views and such). I will probably have to provide a custom view for that.

更新包装元素:

事实证明,它是由Spring编组表示模型的对象的Map所创建的.该映射有一个名为"book"的键(从我认为的类名生成,因为即使我只是返回一本Book也在那里).在我找到更好的方法之前,这里有一种骇人听闻的方法:

It turns out that it is created by Spring marshalling a Map of objects representing the model. This map has a key named "book" (generated from the class name I suppose because its there even if I simply return a Book). Here is a hackish way around it until I can find a better way:

/**
 * When using a Spring Controller that is ignorant of media types, the resulting model
 * objects end up in a map as values. The MappingJacksonJsonView then converts this map
 * to JSON, which (possibly) incorrectly wraps the single model object in the map
 * entry's key. This class eliminates this wrapper element if there is only one model
 * object.
 */
public class SimpleJacksonJsonView extends MappingJacksonJsonView {

    @Override
    @SuppressWarnings("unchecked")
    protected Object filterModel(Map<String, Object> model) {
        Map<String, Object> filteredModel = (Map<String, Object>) super.filterModel(model);
        if(filteredModel.size() != 1) return filteredModel;
        return filteredModel.entrySet().iterator().next().getValue();
    }
}

这篇关于使用Spring MVC 3.0生产/使用对称JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆