首页 \ 问答 \ 如何防止JAX-WS Web方法额外标记(How to Prevent JAX-WS Web Method Extra Tag)

如何防止JAX-WS Web方法额外标记(How to Prevent JAX-WS Web Method Extra Tag)

我试图在java中使用JAX-WS Web方法。 我遇到了一些设计问题。 我试图改变从Web服务方法生成的xml结构。 一些代码部分写在下面。 我希望我可以问我想要什么。

我创建了一个测试java类,如下所示。

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"testKod"
})
@XmlRootElement(name = "testService")
public class TestService {

    @XmlElement(required = true)
    protected String testKod;
    @XmlAttribute(required = true)
    protected String uyeKod;
    @XmlAttribute(required = true)
    protected String islemRefNo;
...
}

我实现了Web服务和方法。

@WebMethod(operationName = "testService")
@WebResult(name="testServiceResponse")
public TestServiceResponse testService(@WebParam(name = "params") TestService params){

    TestServiceResponse response = new TestServiceResponse();
    try {
        response.setUyeKod(params.getUyeKod());
        response.setIslemRefNo(params.getIslemRefNo());
        response.setTestKod(params.getTestKod());
    } catch (Exception e) {
        response.getHata().setAciklama(Convert.fromDBtoTR(TExceptionUtil.getExceptionMessage(e)));
        response.getHata().setHataKodu("100");

    }
    return response;
}   

在使用SAOP-UI测试并导出xsd后,客户端请求如下所示。

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://ws.ddd.ccc.bbb.aaa.com/">
   <soapenv:Header/>
   <soapenv:Body>
      <ws:testService>
        <params  islemRefNo="1212" uyeKod="XXX" >
            <testKod>1212</testKod>
        </params>
      </ws:testService>
    </soapenv:Body>
</soapenv:Envelope>

但我不想看到params标签,我想看到如下

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://ws.ddd.ccc.bbb.aaa.com/">
   <soapenv:Header/>
   <soapenv:Body>
      <ws:testService islemRefNo="1212" uyeKod="XXX" >
            <testKod>1212</testKod>
      </ws:testService>
    </soapenv:Body>
</soapenv:Envelope>

简单地说我的xsd如下;

<xs:complexType name="testService">
  <xs:sequence>
    <xs:element name="params" minOccurs="0">
      <xs:complexType>
        <xs:sequence>
          <xs:element name="testKod" type="xs:string"/>
        </xs:sequence>
        <xs:attribute name="uyeKod" type="xs:string" use="required"/>
        <xs:attribute name="islemRefNo" type="xs:string" use="required"/>
     </xs:complexType>
   </xs:element>
  </xs:sequence>
</xs:complexType>

但我想看起来像那样。 我也在响应xml时面临同样的问题。

 <xs:element name="testService">
      <xs:complexType>
        <xs:sequence>
          <xs:element name="testKod" type="xs:string"/>
        </xs:sequence>
        <xs:attribute name="uyeKod" type="xs:string" use="required"/>
        <xs:attribute name="islemRefNo" type="xs:string" use="required"/>
     </xs:complexType>
</xs:element>

I am trying to use JAX-WS web methods in java. And I encounter with some design issue. I trying to change xml structure which generated from web service method. Some code parts are written below. I hope i can ask what i want.

I create a test java class like below.

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"testKod"
})
@XmlRootElement(name = "testService")
public class TestService {

    @XmlElement(required = true)
    protected String testKod;
    @XmlAttribute(required = true)
    protected String uyeKod;
    @XmlAttribute(required = true)
    protected String islemRefNo;
...
}

And I implement the web service and method.

@WebMethod(operationName = "testService")
@WebResult(name="testServiceResponse")
public TestServiceResponse testService(@WebParam(name = "params") TestService params){

    TestServiceResponse response = new TestServiceResponse();
    try {
        response.setUyeKod(params.getUyeKod());
        response.setIslemRefNo(params.getIslemRefNo());
        response.setTestKod(params.getTestKod());
    } catch (Exception e) {
        response.getHata().setAciklama(Convert.fromDBtoTR(TExceptionUtil.getExceptionMessage(e)));
        response.getHata().setHataKodu("100");

    }
    return response;
}   

After I test with SAOP-UI and export xsd, the client request looks below.

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://ws.ddd.ccc.bbb.aaa.com/">
   <soapenv:Header/>
   <soapenv:Body>
      <ws:testService>
        <params  islemRefNo="1212" uyeKod="XXX" >
            <testKod>1212</testKod>
        </params>
      </ws:testService>
    </soapenv:Body>
</soapenv:Envelope>

But i dont want to see params tag, I want to see like below

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://ws.ddd.ccc.bbb.aaa.com/">
   <soapenv:Header/>
   <soapenv:Body>
      <ws:testService islemRefNo="1212" uyeKod="XXX" >
            <testKod>1212</testKod>
      </ws:testService>
    </soapenv:Body>
</soapenv:Envelope>

Briefly my xsd like below;

<xs:complexType name="testService">
  <xs:sequence>
    <xs:element name="params" minOccurs="0">
      <xs:complexType>
        <xs:sequence>
          <xs:element name="testKod" type="xs:string"/>
        </xs:sequence>
        <xs:attribute name="uyeKod" type="xs:string" use="required"/>
        <xs:attribute name="islemRefNo" type="xs:string" use="required"/>
     </xs:complexType>
   </xs:element>
  </xs:sequence>
</xs:complexType>

But i want to looks like that. Also i faced with same issue in response xml.

 <xs:element name="testService">
      <xs:complexType>
        <xs:sequence>
          <xs:element name="testKod" type="xs:string"/>
        </xs:sequence>
        <xs:attribute name="uyeKod" type="xs:string" use="required"/>
        <xs:attribute name="islemRefNo" type="xs:string" use="required"/>
     </xs:complexType>
</xs:element>

原文:https://stackoverflow.com/questions/42720508
更新时间:2022-07-29 08:07

最满意答案

类和对象是完成任务的工具 - 它们允许您使用一组方法封装数据状态 。 但是,您的数据只是一个数字。 无需封装整数,因此无需创建类。

换句话说,不要创建一个类,因为你认为应该创建一个类,因为它使你的代码更简单。

import sys

def f_to_c(x):
    return (x - 32) * (5/9)

def c_to_f(x):
    return x * (9/5) + 32

num_from = float(input('Enter a number to convert: '))
unit_from = input('What units would you like to convert from? ')
unit_to = input('What units would you like to convert to? ')

if (unit_from, unit_to) == ('fahrenheit', 'celsius'):
    num_to = f_to_c(num_from)
elif (unit_from, unit_to) == ('celsius', 'fahrenheit'):
    num_to = c_to_f(num_from)
else:
    print('unsupported units')
    sys.exit(1)

print('{} degrees {} is {} degrees {}'
      .format(num_from, unit_from, num_to, unit_to))
Enter a number to convert: 40
What units would you like to convert from? celsius
What units would you like to convert to? fahrenheit
40.0 degrees celsius is 104.0 degrees fahrenheit

convert对象和Converter类不起任何作用,因此代码更简单,更容易阅读。


Classes and objects are tools to accomplish a task -- they allow you to encapsulate data or state with a set of methods. However, your data is just a number. There is no need to encapsulate the integer, so there is no need to create a class.

In other words, don't create a class because you think you should, create a class because it makes your code simpler.

import sys

def f_to_c(x):
    return (x - 32) * (5/9)

def c_to_f(x):
    return x * (9/5) + 32

num_from = float(input('Enter a number to convert: '))
unit_from = input('What units would you like to convert from? ')
unit_to = input('What units would you like to convert to? ')

if (unit_from, unit_to) == ('fahrenheit', 'celsius'):
    num_to = f_to_c(num_from)
elif (unit_from, unit_to) == ('celsius', 'fahrenheit'):
    num_to = c_to_f(num_from)
else:
    print('unsupported units')
    sys.exit(1)

print('{} degrees {} is {} degrees {}'
      .format(num_from, unit_from, num_to, unit_to))
Enter a number to convert: 40
What units would you like to convert from? celsius
What units would you like to convert to? fahrenheit
40.0 degrees celsius is 104.0 degrees fahrenheit

The convert object and Converter class don't serve any purpose, so the code is simpler and easier to read without them.

相关问答

更多
  • 我不完全理解他的例子。 然而,重要的是要理解,OOP对于可以自然地建模的东西是非常有效的,对于其他事情(例如横切关注或方面)非常无效。 这是OOP的一个缺点。 另一个原因是由于动态调度,它经常会导致一些运行时费用。 此外,很难滥用OOP做无意义的抽象。 有一个火箭从身体继承是一个例子。 我的经验是,新手开发人员不要信任,也不要使用继承,或者当其他行为(如聚合))更合适时,他们是过度渴望的,并且使用不正确。 随着时间的推移,机制的经验和理解得到改善。 我不知道他的意思是“OOP模式不严格执行继承规则”,因为O ...
  • 你错误地启动了你的课程。 试着这样做: m = Mazzo() m.Crea() 所以快速解释为什么。 首先,第一行初始化一个Mazzo类型的对象并设置为m(请注意'()'你需要为所有方法,初始化或其他方法。)由于它的唯一边界是self它可以留空。 接下来我们要调用Crea函数,我们通过调用刚创建的对象而不是类本身来完成。 You are initilizing your class incorrectly. Try doing this: m = Mazzo() m.Crea() So a quick ...
  • 这是因为Python解释器(CPython)正在进行动态查找以调度所有调用,索引等。动态查找允许语言具有很大的灵活性,但性能成本却很高。 当您使用“方法包装器”时,这(至少)正在发生: 查找mwp.id - 它碰巧是一个方法,但它也只是一个分配给属性的对象,必须像其他任何一样被查找 调用 mwp.id() 在方法内部,查找self._data 查看self._data的__getitem__ 调用 __getitem__ (至少这将是一个C函数,但你仍然必须通过所有这些动态查找来到这里) 相比之下,您的“D ...
  • 取而代之: k = getattr(u, 'self.b') 这样: k = getattr(u, 'b') 或者更好的做法是: k = u.b replace this: k = getattr(u, 'self.b') by this: k = getattr(u, 'b') or even better just do: k = u.b
  • 正如Khelwood在评论中提到的那样,问问自己:“汽车是一种发动机吗?”。 在语义上,继承描述了“是一种”关系 - 如果“B”继承自“A”,则“B”是“A”(参见liskov的替代原则 )。 从这个角度来看,“汽车”不是一个“引擎”,它有一个引擎,所以你想要构图(“有一个”关系)。 从技术上讲 - 特别是在像Python这样的动态类型语言中,你并不严格需要用于子类型 - 继承(我的意思是实现继承)也是一种受限制的静态形式的组合/委托,所以你可能会看到使用继承的代码主要用于实现重用,但这仍然是一个设计POV ...
  • 出错的原因是您尝试返回的属性与属性修饰的方法名称完全相同。 因此,当您调用方法name ,调用会再次触发该方法,因为self.name是在类中声明的方法。 这会触发非终止递归。 而是,更改属性名称: class Person(object): @property def name(self): return self._name @name.setter def name(self, newname): self._name = newname The re ...
  • 它绝对不比功能或程序编程更安全,也不安全。 由于安全性取决于程序员,我没有看到面向对象是如何“更安全”的。 It's definitely not more secure than functional or procedural programming, nor less secure. As security lies in the hands of the programmer, I fail to see how OO is "more secure".
  • 你的库代码清楚地说, SearchResultItem有一个属性.object_id 。 print(reportingRow.results[0].object_id) # this works just fine 您的问题不是字典/ JSON相关,因为您没有使用字典。 您正在使用围绕这些词典的自定义对象。 Your library code clearly says, that SearchResultItem has a property .object_id. print(reportingRo ...
  • 给内心另一个名字: def foo(self, a): def closuer(b): print "closure %d, %d" % (self.val, a) return closuer 而且,而不是使用types.MethodType,您可能想要使用functools.partial Give the inner self another name: def foo(self, a): def closuer(b): print "clos ...
  • 类和对象是完成任务的工具 - 它们允许您使用一组方法封装数据或状态 。 但是,您的数据只是一个数字。 无需封装整数,因此无需创建类。 换句话说,不要创建一个类,因为你认为应该创建一个类,因为它使你的代码更简单。 import sys def f_to_c(x): return (x - 32) * (5/9) def c_to_f(x): return x * (9/5) + 32 num_from = float(input('Enter a number to convert: ' ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)