如何防止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
最满意答案
类和对象是完成任务的工具 - 它们允许您使用一组方法封装数据或状态 。 但是,您的数据只是一个数字。 无需封装整数,因此无需创建类。
换句话说,不要创建一个类,因为你认为应该创建一个类,因为它使你的代码更简单。
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 fahrenheitThe
convert
object andConverter
class don't serve any purpose, so the code is simpler and easier to read without them.
相关问答
更多-
OOP的缺点?(Disadvantage of OOP? [closed])[2021-08-07]
我不完全理解他的例子。 然而,重要的是要理解,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:OOP开销?(Python: OOP overhead?)[2022-11-11]
这是因为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 ...
-
OOP是安全的,但是如何?(OOP is secure but how? [closed])[2023-11-12]
它绝对不比功能或程序编程更安全,也不安全。 由于安全性取决于程序员,我没有看到面向对象是如何“更安全”的。 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". -
OOP - 访问对象数据(OOP - Accessing Object Data)[2023-10-03]
你的库代码清楚地说, 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 ... -
python closure + oop(python closure + oop)[2022-12-30]
给内心另一个名字: 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: ' ...