在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
满意答案
我遇到了同样的问题,暴露了两个版本的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)
如何使用Spring-WS发布具有动态soap:address位置的WSDL定义(How to publish a WSDL definition with dynamic soap:address location using Spring-WS)
Spring-WS:如何在不启动Web服务的情况下生成WSDL?(Spring-WS: How generate the WSDL without having to start the web service?)
Wsdl到Spring Ws(Wsdl to Spring Ws)
Spring-WS端点记录文字包装/解包(Spring-WS endpoints document literal wrapping/unwrapping)
了解spring-ws,与spring-mvc集成以及自动生成wsdl(understand spring-ws, integration with spring-mvc and automatic generation of wsdl)
spring-ws -SOAP端点无法访问(spring-boot with spring-ws -SOAP Endpoint not accessable)
Spring-WS:SimpleWsdl11Definition,具有WSDL的多节点分类(Spring-WS: SimpleWsdl11Definition with a multi-node taxonomy for WSDL)
Spring WS WSDL自动曝光:不遵循xsd导入(Spring WS WSDL automatic exposure : xsd import are not followed)
Spring-ws客户端编程(Spring-ws client side programming)
相关文章
更多Rails 风格指导
Rails常用插件
Rails中的路由功能是如何对应的?
顶 使用Rails plugin weixin_rails_middleware 快速搭建微信营销平台
Rails设置环境变量
rails 单元测试
在Rails中如何打开一个外部URL,并得到该URL的返回结果。
請問:Rails該如何存取SQL2000中的圖像字段
如何在javascript中写rails的helper代码
用 Rails 搭建微信公众平台 API
最新问答
更多如何检索Ember.js模型的所有属性(How to retrieve all properties of an Ember.js model)
maven中snapshot快照库和release发布库的区别和作用
arraylist中的搜索元素(Search element in arraylist)
从mysli_fetch_array中获取选定的值并输出(Get selected value from mysli_fetch_array and output)
Windows Phone上的可用共享扩展(Available Share Extensions on Windows Phone)
如何在命令提示符下将日期设置为文件名(How to set file name as date in command prompt)
如何在Laravel 5.2中使用paginate与关系?(How to use paginate with relationships in Laravel 5.2?)
从iframe访问父页面的id元素(accessing id element of parent page from iframe)
linux的常用命令干什么用的
Feign Client + Eureka POST请求正文(Feign Client + Eureka POST request body)
Copyright ©2023 peixunduo.com All Rights Reserved.粤ICP备14003112号
本站部分内容来源于互联网,仅供学习和参考使用,请莫用于商业用途。如有侵犯你的版权,请联系我们(neng862121861#163.com),本站将尽快处理。谢谢合作!