简体   繁体   English

将Java对象传递给RESTful服务

[英]Passing Java Object to a RESTful service

I'm passing a Java object to a Web service that accepts json, using HttpURLConnection . 我正在使用HttpURLConnection将Java对象传递给接受json的Web服务。 Employing com.fasterxml.jackson to convert the object to json string and then writing it to the output stream, the service works fine for simple dummy POJOs, but the application breaks when I use a complex object that I'm originally intend to send, with the console filled with exceptions like StackOverflow and endless clutter of 使用com.fasterxml.jackson将对象转换为json字符串,然后将其写入输出流,该服务对于简单的虚拟POJO可以正常工作,但是当我使用原本打算发送的复杂对象时,应用程序将中断,控制台中充满了诸如StackOverflow和无尽混乱的异常

at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:690) at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155) 在com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155)上的com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:690)

Common solutions are like annotating the class with json annotation. 常见的解决方案就像用json注释对类进行注释。 Is there any other way to get around it, like using different accept:content-type for web service (other than json or xml as they require annotated objects) or like that? 还有其他解决方法,例如对Web服务使用不同的accept:content-type(除了json或xml,因为它们需要带注释的对象)还是类似的方式?

You can use mixin annotations to provide hints for Jackson on how to serialize your data, without polluting the POJO with unnecessary dependencies. 您可以使用mixin批注为Jackson提供有关如何序列化数据的提示,而不会因不必要的依赖项而污染POJO。

Here's the reference to documentation article, that has good example: http://wiki.fasterxml.com/JacksonMixInAnnotations 这是对文档文章的引用,其中有一个很好的示例: http : //wiki.fasterxml.com/JacksonMixInAnnotations

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM