在Rails 4中格式化我的JSON输出(Pretty format my JSON output in Rails 4)

我在我的控制器中使用pretty_generate,但是我收到以下错误

'只允许生成一代JSON对象或数组'

@celebrities = Celebrity.includes(:category)    
respond_to do |format|
  format.json { render json: JSON.pretty_generate(@celebrities.to_json(:include =>{:category => {:only => [:category]} })) }
end

我不知道为什么我会收到这个错误


I am using pretty_generate in my controller, but I am getting the following error

'only generation of JSON objects or arrays allowed'

@celebrities = Celebrity.includes(:category)    
respond_to do |format|
  format.json { render json: JSON.pretty_generate(@celebrities.to_json(:include =>{:category => {:only => [:category]} })) }
end

I am not sure why I am getting this error


原文:https://stackoverflow.com/questions/32439655
2024-03-26 07:03

满意答案

我遇到了同样的问题,暴露了两个版本的webservice与不同的网址。

old version within http://hostname/ws.wsdl
new version within http://hostname/version/ws.wsdl

我的解决方案不是使用通用的org.springframework.ws.transport.http.MessageDispatcherServletservlet,而是使用默认的org.springframework.web.servlet.DispatcherServlet,并在我的bean配置中配置url映射到不同的wsdl版本。

我更喜欢这个解决方案,因为它可以在不对任何spring类进行子类化的情

web.xml中:

<servlet>
    <servlet-name>webservice</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value></param-value>
    </init-param>
</servlet>

<servlet-mapping>
    <servlet-name>webservice</servlet-name>
    <url-pattern>/ws</url-pattern>
    <url-pattern>/ws.wsdl</url-pattern>
    <url-pattern>/version/ws</url-pattern>
    <url-pattern>/version/ws.wsdl</url-pattern>
</servlet-mapping>

beans.xml中

<bean id="messageFactory" class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory" />

<bean
    class="org.springframework.ws.transport.http.WebServiceMessageReceiverHandlerAdapter">
    <property name="messageFactory" ref="messageFactory" />
</bean>

<bean id="messageDispatcher"
    class="org.springframework.ws.soap.server.SoapMessageDispatcher" />

<bean
    class="org.springframework.ws.transport.http.WsdlDefinitionHandlerAdapter" />

<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
        <props>
            <prop key="/ws.wsdl">ws</prop>
            <prop key="/version/ws.wsdl">ws-newversion</prop>
        </props>
    </property>
    <property name="defaultHandler" ref="messageDispatcher" />
</bean>

<bean id="ws"
    class="org.springframework.ws.wsdl.wsdl11.SimpleWsdl11Definition">
    <constructor-arg value="classpath:wsdl/oldversion/Service.wsdl" />
</bean>

<bean id="ws-newversion"
    class="org.springframework.ws.wsdl.wsdl11.SimpleWsdl11Definition">
    <constructor-arg value="classpath:wsdl/newversion/CarService.wsdl" />
</bean>

因此,每个wsdl都在SimpleUrlHandlerMapping-Bean中配置的给定路径上公开。


After poking around spring-ws source, I found there is no support for exposing a multi-node path for static-sourced WSDL configuration.

So I subclassed MessageDispatcherServlet and SimpleWsdl11Definition, and in my servlet, provided my own WSDL-request mapper that supports existing WsdlDefinition beans, as well as my "location-specified" WsdlDefinition bean.

Yields ability to configure in this sort of manner:

<!-- exposes URL:  host/context-root/servlet-name/MyService.wsdl -->
<bean id="MyService" class="org.springframework.ws.wsdl.wsdl11.SimpleWsdl11Definition">
    <property name="wsdl" value="/WEB-INF/wsdl/MyService.wsdl" />
</bean>

<!-- exposes URL:  host/context-root/servlet-name/some/multi/node/taxonomy/path/MyService.wsdl -->
<bean id="MyService.otherVersion" class="path.to.my.EnhancedWsdl11Definition">
    <property name="wsdl" value="/WEB-INF/wsdl/otherVersion/MyService.wsdl" />
    <property name="locationUri" value="some/multi/node/taxonomy/path/MyService.wsdl" />
</bean>

All is well.

相关问答

更多

如何更改Spring-WS的“SOAP-ENV”默认前缀(How to change “SOAP-ENV” default prefix of Spring-WS)

更好的解决方案 使用SOAPMessage API而不是DOM。 private void alterSoapEnvelope(SaajSoapMessage soapResponse) { try { SOAPMessage soapMessage = soapResponse.getSaajMessage(); SOAPPart soapPart = soapMessage.getSOAPPart(); SOAPEnvelope envelope...

如何使用Spring-WS发布具有动态soap:address位置的WSDL定义(How to publish a WSDL definition with dynamic soap:address location using Spring-WS)

使用这个类: import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.Source; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.sax.SAXSource; import javax.xml.xpath.XPath; impo...

Spring-WS:如何在不启动Web服务的情况下生成WSDL?(Spring-WS: How generate the WSDL without having to start the web service?)

此示例代码适用于Ant任务的基础: import javax.xml.stream.XMLStreamException; import javax.xml.transform.TransformerFactory; import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.Resource; import org.springframework.ws.wsdl.wsdl11...

Wsdl到Spring Ws(Wsdl to Spring Ws)

Spring WS不提供任何实用程序在任何情况下,您都可以使用与spring ws集成的JAXB或JAX-WS一些有用的链接: Spring WS + JAX-WS Spring WS + JAXB(参见使用SOAP) Spring WS doesn't provide any utility In any case you can use JAXB or JAX-WS integrated with spring ws Some usefull links: Spring WS + JAX-WS...

Spring-WS端点记录文字包装/解包(Spring-WS endpoints document literal wrapping/unwrapping)

我们的解决方案是通过我们的jaxb bindings.xml将名称空间和元素名称添加到@XmlRootElement注释中,例如 <jaxb:bindings node="xs:complexType[@name='fooType']"> <annox:annotate target="class"> <annox:annotate annox:class="javax.xml.bind.annotation.XmlRootElement" name="FooAcknowledgeme...

了解spring-ws,与spring-mvc集成以及自动生成wsdl(understand spring-ws, integration with spring-mvc and automatic generation of wsdl)

问题是你的xsd错了。 您应该将复杂类型包装在元素中。 <xs:element name="deliverShortMessageRequest"> <xs:complexType> <xs:sequence> <xs:element name="parameters" minOccurs="0"> <xs:complexType> <xs:sequence> <xs:element name="sms" type...

spring-ws -SOAP端点无法访问(spring-boot with spring-ws -SOAP Endpoint not accessable)

WS不需要MessageDispatcherServlet吗? 所以你需要用其中的一个替换默认的DispatcherServlet ,例如 @Bean public MessageDispatcherServlet dispatcherServlet() { return new MessageDispatcherServlet(); } Doesn't Spring WS need a MessageDispatcherServlet? So you would need to rep...

Spring-WS:SimpleWsdl11Definition,具有WSDL的多节点分类(Spring-WS: SimpleWsdl11Definition with a multi-node taxonomy for WSDL)

我遇到了同样的问题,暴露了两个版本的webservice与不同的网址。 old version within http://hostname/ws.wsdl new version within http://hostname/version/ws.wsdl 我的解决方案不是使用通用的org.springframework.ws.transport.http.MessageDispatcherServletservlet,而是使用默认的org.springframework.web.servle...

Spring WS WSDL自动曝光:不遵循xsd导入(Spring WS WSDL automatic exposure : xsd import are not followed)

解决了! 我不得不使用XsdSchemaCollection而不是SimpleXsdSchema; 另外,我必须将集合的“inline”参数设置为true。 @Bean(name="updateContactService") public DefaultWsdl11Definition defaultWsdl11Definition() throws Exception { DefaultWsdl11Definition wsdl11Definition = new DefaultWsd...

Spring-ws客户端编程(Spring-ws client side programming)

看这里http://blog.dawouds.com/2008/09/maven-2-wsdl-to-java-using-jaxb.html look here http://blog.dawouds.com/2008/09/maven-2-wsdl-to-java-using-jaxb.html

相关文章

更多

Rails 风格指导

感谢译者。 本页用于介绍 Ruby 社区首推的Rails代码编写风格,翻译来自:https://git ...

Rails常用插件

测试驱动 rspec-rails:BDD测试框架 初始化:rails generate ...

Rails中的路由功能是如何对应的?

我才开始接触ROR,我是参照agile web development with rails这本书学习 ...

顶 使用Rails plugin weixin_rails_middleware 快速搭建微信营销平台

weixin_rails_middleware,是专门为Rails项目开发微信第三方营销平台的gem, ...

Rails设置环境变量

目前接触的环境变量分为2种,这里以sunspot中设置solr url为例 1. ENV['SOLR_ ...

rails 单元测试

rails 如何保证每次单元测试都清理上一个测试的数据。

在Rails中如何打开一个外部URL,并得到该URL的返回结果。

需要开发一个接口程序,外部程序提供了一个URL的接口,即访问一个URL,最终会得到相应的执行结果。 ...

請問:Rails該如何存取SQL2000中的圖像字段

我做了一個Rails應用已連接好SQL2000,其中SQL2000的一張表中有一個image類型的字段 ...

如何在javascript中写rails的helper代码

如果在模板中这样写 &lt;SCRIPT language=JavaScript&gt; var ...

用 Rails 搭建微信公众平台 API

最近微信很火,春节在家抽空研究了公众平台的 API ,发现挺有意思的,写篇小文,简单记录一下 微信 A ...

最新问答

更多

如何检索Ember.js模型的所有属性(How to retrieve all properties of an Ember.js model)

您可以简单地在模型上使用toJSON方法并从对象中获取密钥。 Ember.keys(model.toJSON()) 请注意,不会返回关键字。 You can simply use toJSON method on model and get the keys from object. Ember.keys(model.toJSON()) Note that will not return you keys for relations.

maven中snapshot快照库和release发布库的区别和作用

在使用maven过程中,我们在开发阶段经常性的会有很多公共库处于不稳定状态,随时需要修改并发布,可能一天就要发布一次,遇到bug时,甚至一天要发布N次。我们知道,maven的依赖管理是基于版本管理的,对于发布状态的artifact,如果版本号相同,即使我们内部的镜像服务器上的组件比本地新,maven也不会主动下载的。如果我们在开发阶段都是基于正式发布版本来做依赖管理,那么遇到这个问题,就需要升级组件的版本号,可这样就明显不符合要求和实际情况了。但是,如果是基于快照版本,那么问题就自热而然的解决了

arraylist中的搜索元素(Search element in arraylist)

您正在尝试搜索主题的arraylist,您需要编写一个小函数来将代码字符串与类的相应字符串进行比较。 您可以通过将其添加到主题类来完成此操作。 示例: @Override public boolean equals(String code) { return code.equals(this.); } 并将比较更改为需要匹配您匹配的代码的成员。 编辑:更简单的方法是将现有代码更改为: if (s.code.equals(codeNo[i])) //

从mysli_fetch_array中获取选定的值并输出(Get selected value from mysli_fetch_array and output)

这当然是一个重复的问题 。 编辑 : 循环执行后, $gg将指向列表中的最后一个值(在本例中为1002)。 我相信您正在尝试访问的用户选择的的值,可以通过以下方式完成: 在edit.php中 : "; while

Windows Phone上的可用共享扩展(Available Share Extensions on Windows Phone)

不可以。您只能作为图片的共享提供商。 对于其他所有内容,您可以为开发人员提供一个可以与您的应用关联的URI方案 ,因此如果他们决定与您分享内容,则可以稍后调用它。 但是,这不是系统范围的共享扩展。 No. You can only act as a share provider for pictures. For everything else, you can give developers an URI scheme that they can associate with your app

如何在命令提示符下将日期设置为文件名(How to set file name as date in command prompt)

首先,对于你的路径字符串,你应该将r前缀作为原始字符串,并且\不被视为转义字符。 其次,您可以使用datetime.datetime.now()来获取当前时间,然后使用strftime()来填充日期。 示例 - import datetime path = r'C:\soa11g\New\abc_%s.zip' % datetime.datetime.now().strftime('%d%m%Y') connect('weblogic','welcome','t3://localhost:700

如何在Laravel 5.2中使用paginate与关系?(How to use paginate with relationships in Laravel 5.2?)

请尝试以下方法: $messages = $user->contact_messages()->paginate(10); Try the following: $messages = $user->contact_messages()->paginate(10);

从iframe访问父页面的id元素(accessing id element of parent page from iframe)

您试图在父窗口上使用jQuery,尽管它没有在该上下文中定义。 它仅在子文档中定义。 而是更改您的子iframe脚本以在父窗口的元素上调用jQuery。 这是一个例子: 父文件 Parent click

linux的常用命令干什么用的

linux和win7不一样,win7都图形界面,都是用鼠标来操作打开,解压,或者关闭。而linux是没有图形界面的,就和命令提示符一样的一个文本框,操作linux里的文件可没有鼠标给你用,打开文件夹或者解压文件之类的操作都要通过linux常用命令。

Feign Client + Eureka POST请求正文(Feign Client + Eureka POST request body)

问题是Feign接口中的方法不能有多个“通用”参数。 您可以拥有任意数量的标头参数,但不能多于一个主体。 由于@RequestBody没有做任何事情,因此除了HttpServletRequest请求变量之外,它不被视为标题而是另一个变量。 所以我不得不改变我的业务逻辑只有一个参数。 The problem was that a method in Feign interface cannot have more than one 'general' argument. you can have