我有一个用于遗留数据库的POJO,我为它创建了这个脚手架:
package example
import com.example.entities.ModelView
class ModelViewController {
static scaffold = ModelView
}当我在http://example.com:8080/example/modelView/list上打开我的浏览器时,我看到了一个非常棒的超文本标记语言页面,在那里我得到了完整的CRUD。太棒了!
现在我想获取XML,所以我访问了URL,但它仍然返回http://example.com:8080/example/modelView/list.xml。
我的最终目标是让Jersey代码在Java应用程序中获得XML,如下所示:
WebTarget targetBase = ClientBuilder.newClient().target("http://example.com:8080/example");
targetBase.path("modelView").path("list.xml").request(MediaType.APPLICATION_XML_TYPE).get(new GenericType<List<ModelView>>(){});因此,我有上面的代码,但是它从grails获得HTML,而不是它所请求的XML。
如何让Grails发送XML?
发布于 2013-12-18 13:25:22
如果您想要所有ModelView实例的XML
def xmllist = {
render ModelView.list() as XML
}有关grails转换器参考,请参阅以下链接
http://grails.org/Converters+Reference
另外,如果您想从相同的list操作中获取数据,可以使用下面的方法
import grails.converters.XML
class ModelViewController {
def list() {
def modelViews = ModelView.list()
withFormat {
html modelViews :modelViews
js { render "alert('hello')" }
xml { render modelViews as XML }
}
}
} 除非您将grails.mime.use.accept.header = true设置添加到Config.groovy文件中,否则Grails将忽略HTTP Accept header。换句话说,如果没有该设置,withFormat()将完全不受Accept header的影响。
发布于 2013-12-18 11:38:16
你可以这样做的一种方法是安装grails Scaffolding plugin (http://grails.org/doc/latest/guide/scaffolding.html),它允许你覆盖默认的控制器和视图模板,这些模板被用来生成你所描述的带有完整CRUD的‘美妙的HTML页面’。然后,您将能够在控制器模板中的操作上添加Jersey代码,并且您为其生成的每个新页面都将具有此操作。
万事如意。
发布于 2013-12-18 15:24:09
Grails withFormat就是你想要的。
不要忘记将grails.mime.use.accept.header配置的值设置为true
grails.mime.use.accept.header = truehttps://stackoverflow.com/questions/20649122
复制相似问题