如何在Java中获取和执行复杂对象的操作,如XML

时间:2015-03-20 06:42:55

标签: java xml web-services object

我必须创建一个自下而上的webservice,我将创建一个类来接受XML之类的复杂对象作为输入并对其执行操作。你能否为我提供一个解决方案,如何接受复杂的对象而不是webservice中的java中的String。

import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;

@WebService(serviceName = "HelloWorld")
public class HelloWorld {

    /**
     * This is a sample web service operation
     */
    @WebMethod(operationName = "hello")
    public String hello(@WebParam(name = "name") String xml) {

        // My implementation to parse the xml
        return "This xml contains the following value" + txt + " !";
    }
}

我不想将xml作为字符串并且想要使用复杂对象。因为我必须将String构造成file.xml并再次解析并再次转换为字符串。

1 个答案:

答案 0 :(得分:0)

使用JAXB - 使用注释可以创建对象到XML映射,反之亦然。用法类似于JPA / Hibernate Entity Annotation Mapping。

https://jaxb.java.net/

创建映射类后,您将能够声明类似

的方法
 public MyCustomRequestResponse hello(@WebParam MyCustomRequest request) {