且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将java对象作为参数传递给restful webservice

更新时间:2023-11-07 21:01:52

您可以使用三种典型选项。

There are three typical options available to you.

将对象变量传递给请求

如果您没有大量变量,或者只需要填充Registration_BE中的一部分字段,这将非常有用。

This is useful if you do not have a large number of variables, or need the ability to populate only a subset of the fields in Registration_BE.

如果你想将变量作为典型的POST传递给请求,你需要做一些处理来构建复杂的 Registration_BE 对象:

If you want to pass the variables into the request as a typical POST, you will need to do some processing to construct the complex Registration_BE object in the first place:

public String getText(@RequestParam("reg_be.myvariable") String myvariable) {
   Registration_BE reg_be = new Registration_BE(myvariable);

   System.out.println("websevice:" +reg_be.myvariable);

   return reg_be.myvariable;
}

您可以通过以下方式拨打电话:

And you can call it with:

http://192.168.1.1:8084/UnionClubWS/webresources/customerregistration/?reg_be.myvariable=myvalue

或者通过传入一系列变量:

Or alternatively by passing in an array of variables:

public String getText(@RequestParam("reg_be.myvariable") String[] myvariables) {
   Registration_BE reg_be = new Registration_BE(myvariables);

   System.out.println("websevice:" +reg_be.myvariable);

   return reg_be.myvariable;
}

您可以通过以下方式拨打电话:

And you can call it with:

http://192.168.1.1:8084/UnionClubWS/webresources/customerregistration/?reg_be.myvariable=myvalue1&reg_be.myvariable=myvalue2

使用通用数据交换格式

第二个选项是将注册对象作为JSON(或XML)传递。为此,你需要启用Jackson消息转换器并确保杰克逊库在您的类路径中:

The second option would be to pass your registration object as JSON (or XML). for this, you will need to enable the Jackson message convertor and make sure the Jackson library is in your classpath:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <mvc:annotation-driven />

</beans>

您的方法不会改变:

public String getText(@RequestParam("reg_be") Registration_BE reg_be ) {

   System.out.println("websevice:" +reg_be.myvariable);

   return reg_be.myvariable;
}

现在可以用以下方式调用它:

And you can now call it with:

http://192.168.1.1:8084/UnionClubWS/webresources/customerregistration/?reg_be={"myvariable":"myvalue"}

自定义邮件转换器

您的第三个,最复杂的选择是创建自己的消息转换器。这将为您提供最大的灵活性(您的请求可以采用您喜欢的任何形式),但会涉及更多的样板开销以使其工作。

Your third, and most complex, option would be to create your own message convertor. This would give you the most flexibility (your request could take any form you like), but would involve a lot more boilerplate overhead to make it work.

除非您对如何构建请求数据包有非常具体的要求,否则我建议您选择上述选项之一。

Unless you have a very specific requirement on how the request packet should be constructed, I recommend you opt for one of the above options.