本文介绍了如何使用JAXB和Spring @ResponseBody在控制器中生成正确的sitemap命名空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
一切正常工作正常的异常,我不能正确创建命名空间。非常感谢任何帮助!
Everything is working fine with the exception that I cannot create the namespace correctly. Any help is much appreciated!
我的控制器:
@Controller
@RequestMapping("/sitemap")
public class SitemapController
{
public @ResponseBody XMLURLSet getSitemap(){
XMLURLSet urlSet = new XMLURLSet();
//populate urlList
urlSet.setUrl(urlList);
return urlSet;
}
}
我的网址集:
@XmlRootElement(name = "url")
public class XMLURL {
String loc;
@XmlElement(name = "loc")
public String getLoc(){
return loc;
}
public void setLoc(String loc){
this.loc = loc;
}
}
我的网址元素:
@XmlRootElement(name = "urlset", namespace = "http://www.sitemaps.org/schemas/sitemap/0.9")
public class XMLURLSet{
List<XMLURL> url;
public List<XMLURL> getUrl(){
return url;
}
public void setUrl(List<XMLURL> url){
this.url = url;
}
}
我期望生成的:
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>http://www.example.com/</loc>
</url>
产生的结果:
<ns2:urlset xmlns:ns2="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>http://www.example.com/</loc>
</url>
</ns2:urlset>
</urlset>
$ b
谢谢!
Thanks!
推荐答案
您可以利用 @XmlSchema
注释来指定elementFormDefault是合格的。这将有助于您的用例。
You can leverage the @XmlSchema
annotation to specify elementFormDefault is qualified. This should help with your use case.
@XmlSchema(
namespace = "http://www.sitemaps.org/schemas/sitemap/0.9",
elementFormDefault = XmlNsForm.QUALIFIED)
package example;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;
详情
- http://blog.bdoughan.com/2010/08/jaxb-namespaces.html
这篇关于如何使用JAXB和Spring @ResponseBody在控制器中生成正确的sitemap命名空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!