在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.
相关问答
更多-
更好的解决方案 使用SOAPMessage API而不是DOM。 private void alterSoapEnvelope(SaajSoapMessage soapResponse) { try { SOAPMessage soapMessage = soapResponse.getSaajMessage(); SOAPPart soapPart = soapMessage.getSOAPPart(); SOAPEnvelope envelope = so ...
-
使用这个类: 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; import ja ...
-
Spring-WS:如何在不启动Web服务的情况下生成WSDL?(Spring-WS: How generate the WSDL without having to start the web service?)[2023-05-01]
此示例代码适用于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.Defa ... -
Wsdl到Spring Ws(Wsdl to Spring Ws)[2022-03-23]
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 Spri ... -
我们的解决方案是通过我们的jaxb bindings.xml将名称空间和元素名称添加到@XmlRootElement注释中,例如问题是你的xsd错了。 您应该将复杂类型包装在元素中。WS不需要MessageDispatcherServlet吗? 所以你需要用其中的一个替换默认的DispatcherServlet ,例如 @Bean public MessageDispatcherServlet dispatcherServlet() { return new MessageDispatcherServlet(); } Doesn't Spring WS need a MessageDispatcherServlet? So you would need to replace ...我遇到了同样的问题,暴露了两个版本的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.Dis ...
Spring WS WSDL自动曝光:不遵循xsd导入(Spring WS WSDL automatic exposure : xsd import are not followed)[2022-02-10]
解决了! 我不得不使用XsdSchemaCollection而不是SimpleXsdSchema; 另外,我必须将集合的“inline”参数设置为true。 @Bean(name="updateContactService") public DefaultWsdl11Definition defaultWsdl11Definition() throws Exception { DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11De ...看这里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 风格指导
- Rails常用插件
- Rails中的路由功能是如何对应的?
- 顶 使用Rails plugin weixin_rails_middleware 快速搭建微信营销平台
- Rails设置环境变量
- rails 单元测试
- 在Rails中如何打开一个外部URL,并得到该URL的返回结果。
- 請問:Rails該如何存取SQL2000中的圖像字段
- 如何在javascript中写rails的helper代码
- 用 Rails 搭建微信公众平台 API
最新问答
更多- Runnable上的NetworkOnMainThreadException(NetworkOnMainThreadException on Runnable)
- C ++ 11 + SDL2 + Windows:多线程程序在任何输入事件后挂起(C++11 + SDL2 + Windows: Multithreaded program hangs after any input event)
- AccessViolationException未处理[VB.Net] [Emgucv](AccessViolationException was unhandled [VB.Net] [Emgucv])
- 计算时间和日期差异(Calculating Time and Date difference)
- 以编程方式标签NSMutableAttributedString swift 4(Label NSMutableAttributedString programmatically swift 4)
- C#对象和代码示例(C# objects and code examples)
- 在python中是否有数学nCr函数?(Is there a math nCr function in python? [duplicate])
- 检索R中列的最大值和第二个最大值的行名(Retrieve row names of maximum and second maximum values of a column in R)
- 给定md5哈希时如何查找特定文件(How to find specific file when given md5 Hash)
- Python字典因某些原因引发KeyError(Python Dictionary Throwing KeyError for Some Reason)
- 如何让Joomla停止打开新标签中的每个链接?(How do I get Joomla to stop opening every link in a new tab?)
- DNS服务器上的NS记录不匹配(Mismatched NS records at DNS server)
- Python屏幕捕获错误(Python screen capture error)
- 如何在帧集上放置div叠加?(How to put a div overlay over framesets?)
- 页面刷新后是否可以保留表单(html)内容数据?(Is it possible to retain the form(html) content data after page refreshed?)
- 使用iTeardownMyAppFrame和iStartMyAppInAFrame在OPA5测试中重新启动应用程序超时(Restart app within OPA5 test using iTeardownMyAppFrame and iStartMyAppInAFrame timed out)
- 自动拆分文本内容到列(Automatically splitting text content into even columns)
- 在r中的循环中将模型名称分配给gbm.step(assigning model names to gbm.step in loop in r)
- 昆明哪里有电脑等级考试二级C培训?
- C ++模板实例化,究竟是什么意思?(C++ template instantiation, what exactly does it mean?)
- 帮助渲染来自fields_for的部分内容(Help to render a partial from fields_for)
- 将url.action作为json对象返回mvc(return url.action as json object mvc)
- 使用.BAT中的.application文件类型运行ac#Console App(Run a c# Console App with .application file type from a .BAT)
- 将bindingRedirect添加到.Net标准库(Adding a bindingRedirect to a .Net Standard library)
- Laravel版本升级会影响您的控制器吗?(Laravel version upgrade affects your controller?)
- imaplib.error:命令SEARCH在状态AUTH中非法,只允许在SELECTED状态(imaplib.error: command SEARCH illegal in state AUTH, only allowed in states SELECTED)
- 如何在eclipse debug impala前端
- 如何通过Ajax API处理多个请求?(How to handle multiple requests through an Ajax API? [closed])
- 使用Datetime索引来分析数据框数据(Using Datetime indexing to analyse dataframe data)
- JS 实现一个菜单效果