java 如何使用 Webservices 传递像对象这样的复杂类型?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3685711/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 02:59:55  来源:igfitidea点击:

How can I pass complex types like objects using Webservices?

javaweb-servicesserializationsoapsoa

提问by Rachel

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 ? 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 并且正在传递字符串和整数类型,但我不确定如何使用 webservice 传递对象,因此任何指导将不胜感激。

Thanks.

谢谢。

回答by heb

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,您可以尝试 GSON by Google,它提供了一个非常易于使用的编程接口。

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),请查看这些入门教程:

回答by Qwerky

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. Jersey will automatically serialize and deserialize your objects on both the client and server side.

如果您使用的是 Restful Web 服务(如果您是http://jersey.dev.java.net,我会推荐 Jersey )您可以传递 JAXB 注释对象。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 豆;

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

    //Constructors and getters/setters here
}

回答by Bob

You could pass json or use xmlserialization if needed.

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