Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 具有不同xml名称的相同响应对象_Java_Xml_Rest_Jaxb_Marshalling - Fatal编程技术网

Java 具有不同xml名称的相同响应对象

Java 具有不同xml名称的相同响应对象,java,xml,rest,jaxb,marshalling,Java,Xml,Rest,Jaxb,Marshalling,我在一个服务中编写了两个基于Rest的web方法: Response doSomething() ; Response doSomething2() 现在,我想将这些响应封送为两个不同的xml名称。因此,响应如下所示: **<doSomethingResponse>** for doSomething() **<doSomething2Response>** for doSomething2() ****用于doSomething() ****对于

我在一个服务中编写了两个基于Rest的web方法:

 Response doSomething() ;

 Response doSomething2()
现在,我想将这些响应封送为两个不同的xml名称。因此,响应如下所示:

 **<doSomethingResponse>**   for doSomething()

 **<doSomething2Response>**   for doSomething2()
****用于doSomething()
****对于doSomething2()

最好的方法是什么。我使用jaxB进行编组。

您可以使用
@WebResult
@xmlement
注释:

@WebResult(name="doSomethingResponse")
//or
//@XmlElement(name="doSomethingResponse")
Response doSomething();

@WebResult(name="doSomething2Response")
//or
//@XmlElement(name="doSomething2Response")
Response doSomething2();

您可以使用
@WebResult
@xmlement
注释:

@WebResult(name="doSomethingResponse")
//or
//@XmlElement(name="doSomethingResponse")
Response doSomething();

@WebResult(name="doSomething2Response")
//or
//@XmlElement(name="doSomething2Response")
Response doSomething2();

您可以利用
JAXBElement
来提供根元素名称。在下面的示例中,我们将使用
JAXBElement
创建响应
FOO
的根节点

   @GET
    @Produces(MediaType.APPLICATION_XML)
    public Response get() {
        Customer customer = new Customer();
        customer.setFirstName("Jane");
        customer.setLastName("Doe");
        JAXBElement<Customer> jaxbElement = new JAXBElement(new QName("FOO"), Customer.class, customer);
        return Response.ok(jaxbElement).build();
    }
@GET
@生成(MediaType.APPLICATION\u XML)
公众反应{
客户=新客户();
customer.setFirstName(“Jane”);
customer.setLastName(“Doe”);
JAXBElement=newjaxbelement(newqname(“FOO”)、Customer.class、Customer);
返回Response.ok(jaxbElement.build();
}

您可以利用
JAXBElement
来提供根元素名称。在下面的示例中,我们将使用
JAXBElement
创建响应
FOO
的根节点

   @GET
    @Produces(MediaType.APPLICATION_XML)
    public Response get() {
        Customer customer = new Customer();
        customer.setFirstName("Jane");
        customer.setLastName("Doe");
        JAXBElement<Customer> jaxbElement = new JAXBElement(new QName("FOO"), Customer.class, customer);
        return Response.ok(jaxbElement).build();
    }
@GET
@生成(MediaType.APPLICATION\u XML)
公众反应{
客户=新客户();
customer.setFirstName(“Jane”);
customer.setLastName(“Doe”);
JAXBElement=newjaxbelement(newqname(“FOO”)、Customer.class、Customer);
返回Response.ok(jaxbElement.build();
}

您需要澄清希望响应在xml结构的哪一部分有所不同。通常,您应该有两个不同的字符串对象来表示两个不同的响应对象。您需要澄清希望响应在xml结构的哪一部分有所不同。通常情况下,应该有两个不同的字符串对象来表示两个不同的响应对象。WebResult适用于jaxws。XmlElement不工作。它采用xmlRootElement中的名称。我错过了这里的smoething吗?WebResult是为jaxws准备的。XmlElement不工作。它采用xmlRootElement中的名称。我是不是错过了斯摩辛?谢谢,同样的,我也想做完全相反的事情。具有不同xml名称的请求(相同的结构,只有根元素名称不同)应该映射到同一个类。具有不同xml名称的请求(相同的结构,只有根元素名称不同)应该映射到同一个类。