简体   繁体   English

如何使用Webservices传递对象等复杂类型?

[英]How can I pass complex types like objects using Webservices?

This may sound like a simple question but as am newbie in Webservies and this is my first time using it and so am asking my doubt. 这可能听起来像一个简单的问题,但作为Webservies的新手,这是我第一次使用它,所以我怀疑。

Q: How can I pass objects or complex types using Web Services ? 问:如何使用Web服务传递对象或复杂类型? I have created a simple webservice and am passing string and integer types but I am not sure how can I pass objects using webservice and so any guidance would be highly appreciated. 我创建了一个简单的Web服务,并传递字符串和整数类型,但我不知道如何使用webservice传递对象,因此任何指导都将受到高度赞赏。

Thanks. 谢谢。

You only have to serialize the object (make a text) on the service-side and de-serialze (make an object again) on the receiver-side. 您只需要在服务端序列化对象(生成文本)并在接收器端反序列化(再次生成对象)。 For years SOAP was standard for this, but today JSON becomes more popular as it has lot less overhead than SOAP. 多年来,SOAP是标准配置,但是今天JSON变得越来越流行,因为它比SOAP要少得多。

If using SOAP and Java you may try GSON by Google, which provides a very easy-to-use programming interface. 如果使用SOAP和Java,您可以尝试Google的GSON,它提供了一个非常易于使用的编程接口。

JSON with GSON: JSON与GSON:

String jsonized = new Gson().toJson( myComplexObject ); 
/* no we have a serialized version of myComplexObject */ 

myComplexObjectClass myComplexObjext = new Gson().fromJson( jsonized, myComplexObjectClass.class ); 
/* now we have the object again */

For JSON with JAX-WS (we don't use Apache Axis) have a look at these starter-tutorials: 对于使用JAX-WS的JSON(我们不使用Apache Axis),请查看这些入门教程:

If you are using restful web services (I'd recommend Jersey if you are http://jersey.dev.java.net ) you can pass JAXB annotated objects. 如果您使用的是restful Web服务(如果您是http://jersey.dev.java.net,我会推荐Jersey),您可以传递JAXB注释对象。 Jersey will automatically serialize and deserialize your objects on both the client and server side. Jersey将自动在客户端和服务器端序列化和反序列化您的对象。

Server side; 服务器端;

@Path("/mypath")
public class MyResource
{
    @GET
    @Produces(MediaType.APPLICATION_XML)
    public MyBean getBean()
    {
        MyBean bean = new MyBean();
        bean.setName("Hello");
        bean.setMessage("World");
        return bean;
    }

    @POST
    @Consumers(MediaType.APPLICATION_XML)
    public void updateBean(MyBean bean)
    {
        //Do something with your bean here
    }
}

Client side; 客户端;

//Get data from the server
Client client = Client.create();
WebResource resource = client.resource(url);
MyBean bean = resource.get(MyBean.class);

//Post data to the server
bean.setName("Greetings");
bean.setMessage("Universe");
resource.type(MediaType.APPLICATION_XML).post(bean);

JAXB bean; JAXB bean;

@XmlRootElement
public class MyBean
{
    private String name;
    private String message;

    //Constructors and getters/setters here
}

如果需要,您可以传递json或使用xmlserialization。

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

相关问题 管道<I / O>流。我可以传递复杂的物体吗? - Piped<I/O>Stream. Can I pass complex objects? 我可以将复杂的对象作为输入传递到Spring WebFlow子流吗? - Can I pass complex objects as inputs to Spring WebFlow Subflows? 如何使用JPA 2在存储过程中将复杂对象数组作为IN参数传递 - How to pass Array of complex objects as a IN parameter in a stored procedure using JPA 2 如何将复杂对象从控制器传递到视图(jsp) - How can I pass a complex object from controller to the view (jsp) 如何在JPA中使用策略对象保留复杂的实体? - How can I persist complex entity with Strategy objects in JPA? 如何将复杂对象的ArrayList存储到xml中? - How can i store the ArrayList of complex objects into xml? 如何使用instanceof但使用接口类型比较对象(不传递确切的类名。) - How can I compare objects using instanceof but Interface types (not passing exact class name.) 如何使用图形类在Java中绘制复杂的对象? - How do I draw complex objects in Java using the graphics class? Java和C#之间的Web服务-复杂的数据类型 - Webservices between Java & c# - complex data types 我们可以将Java复杂对象(对象内部的对象)传递给存储过程吗? 怎么样? - Can we pass java complex objects(object inside object) to stored procedure? how?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM