Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.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
Java 基于后缀的Spring return JSON/XML/JSON-P_Java_Json_Spring_Spring Mvc - Fatal编程技术网

Java 基于后缀的Spring return JSON/XML/JSON-P

Java 基于后缀的Spring return JSON/XML/JSON-P,java,json,spring,spring-mvc,Java,Json,Spring,Spring Mvc,我已设置了控制器,以便它将通过客户端设置的HTTP Accept Type标头以请求的格式返回数据: <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="jackson

我已设置了控制器,以便它将通过客户端设置的HTTP Accept Type标头以请求的格式返回数据:

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
        <list>
            <ref bean="jacksonJSONMessageConverter" />
            <ref bean="jaxbXMLConverter" />
            <ref bean="jsonpMessageConverter" />
        </list>
    </property>
</bean>
例如,他们会这样做:http://someurl/test

如果客户端可以实际设置接受类型,那么它就可以正常工作。现在,当客户端无法设置Accept Type标头时,问题就从这里开始,我将依赖url作为后缀,例如:

  • http://someurl/test.xml
  • http://someurl/test.json
  • http://someurl/test.jsonp?callback=fn
我的挑战是如何正确配置Spring来实现这一点

一些建议:

  • 使用一些静态方法返回json:
  • 使用响应性
  • 使用默认视图:
还有许多其他的,但是没有一个解决方案能够满足我在一个好的,干净的为什么中所需要的。理想情况下,我希望能够做一些干净的事情,比如

@RequestMapping(value = "/test.xml", method = RequestMethod.POST)
@ResponseBody
public TestObject executeTestReturnXML()
{
    TestObject t = executeTest();

    return t; // somehow magically force Spring converter to convert it to XML
}

@RequestMapping(value = "/test.json", method = RequestMethod.POST)
@ResponseBody
public TestObject executeTestReturnJson()
{
    TestObject t = executeTest();

    return t; // somehow magically force Spring converter to convert it to JSON
}

@RequestMapping(value = "/test.jsonp", method = RequestMethod.POST)
@ResponseBody
public TestObject executeTestReturnJsonP(@RequestParam(value = "callback", required = true) String callback)
{
    TestObject t = executeTest();

    return t; // somehow magically force Spring converter to convert it to JSON-P with callback wrapper
}

建议和/或指导将不胜感激

您可以使用Spring3.1引入的@products注释。看一下实际的文档
在Spring中,Jira()作为一些背景介绍了Spring MVC 3.0+,它具有您所寻求的确切功能

ViewResolver的实现,它基于请求文件名或Accept标头解析视图


这篇博文可以帮你一臂之力:

谢谢!这非常有帮助,我在阅读了两篇博文后就开始工作了。如果使用@ResponseBody注释,则viewsolver是不相关的。(我不确定)你确实是对的。问题是@ResponseBody对于给定的目标可能不够灵活。ContentNegotingViewResolver为您提供了这种灵活性。
@RequestMapping(value = "/test.xml", method = RequestMethod.POST)
@ResponseBody
public TestObject executeTestReturnXML()
{
    TestObject t = executeTest();

    return t; // somehow magically force Spring converter to convert it to XML
}

@RequestMapping(value = "/test.json", method = RequestMethod.POST)
@ResponseBody
public TestObject executeTestReturnJson()
{
    TestObject t = executeTest();

    return t; // somehow magically force Spring converter to convert it to JSON
}

@RequestMapping(value = "/test.jsonp", method = RequestMethod.POST)
@ResponseBody
public TestObject executeTestReturnJsonP(@RequestParam(value = "callback", required = true) String callback)
{
    TestObject t = executeTest();

    return t; // somehow magically force Spring converter to convert it to JSON-P with callback wrapper
}