@ResponseBody没有产生XML在Java中6 [英] @ResponseBody not producing XML in Java 6

查看:251
本文介绍了@ResponseBody没有产生XML在Java中6的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Spring MVC 3 / J2EE项目。在JSP呈现控制器工作正常,但一个控制器呈现XML为Ajax是行不通的。我使用JDK 1.6中的RAD 7.5所以JAXB应在类路径中,我甚至试着加入了最新的JAXB罐子到lib文件,以确保。 我仍然获得了406错误,当我打这个电话我的道场呼叫有 handleAs:XML,我已经确认了应用程序/ XML 是通过萤火的接受头。我有< MVC:注解驱动/> 在我的春天的servlet的XML文件中的行。我可以看到被调用的方法和返回没有错误。我不知道我接下来应该尝试调试。

  //国家一类只灵长类动物的类型,它实现Serializable接口。
公共@ResponseBody名单,其中,国家> getCountries(){
    返回addressService.getCountries();
}
 


 函数loadData(){
    的console.log(以前获得......);
    dojo.xhrGet({
        网址:HTTP://本地主机:9080 /样品/共享/ getCountries.htm
        handleAs:XML,
        负载:功能(数据){
            的console.log(在装载功能...);
            尝试 {
                对于(数据变种I){
                   的console.log(钥匙,我,价值,数据[I]);
                }
            }赶上(前){
                console.error(失败的装载功能:+前);
            }
            的console.log(退出装载功能...);
        },
        错误:函数(X){
            console.error(在阿贾克斯的错误......);
            console.error(X);
        },
        failOk:假的
    });
    的console.log(得到后......);
}
 

解决方案

尝试创建以下包装类:

  @XmlRootElement
一流的国家{
    私人列表<国家>国家=新的ArrayList<国家>()

    // getter / setter方法
}
 

和它的控制器,而不是一个原始的列表返回:

 公共@ResponseBody国家getCountries()
 

最有可能的问题是由JAXB是不能编组Java的列表(它不知道如何命名的XML文档的根标签)。需要注意的是,要求在JSON数据时(如果杰克逊可以在您的CLASSPATH)你的问题可能就不会发生。

又见(类似的问题):

I have a Spring MVC 3/J2EE Project. The jsp rendering controllers are working fine, but the one controller that renders XML for Ajax is not working. I am using JDK 1.6 in RAD 7.5 so JAXB should be on the classpath, and I've even tried adding the latest JAXB jars to the lib file to make sure. I still get a 406 error when I make the call. My DOJO call has handleAs: "xml", and I've confirmed that application/xml is on the Accept header via FireBug. I have the <mvc:annotation-driven /> line in my spring servlet xml file. I can see the method being invoked and returning without error. I'm not sure what I should try next to debug.

//Country is a class with only primative types which implements Serializable.
public @ResponseBody List<Country> getCountries(){
    return addressService.getCountries();
}


function loadData(){
    console.log("Before get ...");
    dojo.xhrGet({
        url:"http://localhost:9080/sample/shared/getCountries.htm",
        handleAs:"xml",
        load: function(data){
            console.log("In load function ...");
            try {
                for(var i in data){
                   console.log("key", i, "value", data[i]);
                }
            }catch (ex){
                console.error("Failure in load function: " + ex);
            }
            console.log("Exiting load function ...");
        },
        error: function(x){
            console.error("Error in ajax ...");
            console.error(x);
        },
        failOk: false
    });
    console.log("After get ...");
}

解决方案

Try creating the following wrapper class:

@XmlRootElement
class Countries {
    private List<Country> countries = new ArrayList<Country>()

    //getters/setters
}

And return it from the controller instead of a raw list:

public @ResponseBody Countries getCountries()

Most likely your problem is caused by JAXB that is unable to marshal Java list (it does not know how to name the root tag of the XML document). Note that your problem probably does not occur when requesting data in JSON (if Jackson is available on your CLASSPATH).

See also (similar problems):

这篇关于@ResponseBody没有产生XML在Java中6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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