如何在控制器中使用 JAXB 和 Spring @ResponseBody 生成正确的站点地图命名空间? [英] How to generate the correct sitemap namespace using JAXB and Spring @ResponseBody in controller?
本文介绍了如何在控制器中使用 JAXB 和 Spring @ResponseBody 生成正确的站点地图命名空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
一切正常,但我无法正确创建命名空间.非常感谢任何帮助!
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>
谢谢!
推荐答案
您可以利用 @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;
了解更多信息
这篇关于如何在控制器中使用 JAXB 和 Spring @ResponseBody 生成正确的站点地图命名空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文