使用@WebParm
soap请求无论是接收Header还是Body中的key-value值都是使用@WebParam这个注解,区别是接收Header时,要把参数header设置为true(默认为false)。
@WebParam(name = "hostAddress", header = true) 捕捉header中的key-value
@WebParam(name = "username") 捕捉body中的key-value
举例说明
下面是soap请求示例
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:log="http://LoginService">
<soapenv:Header>
<log:hostAddress>abc</log:hostAddress>
<log:macAddress>tan</log:macAddress>
</soapenv:Header>
<soapenv:Body>
<log:login>
<username>naoms</username>
<password>123456</password>
</log:login>
</soapenv:Body>
</soapenv:Envelope>
下面是服务端接收参数示例:
@WebService(name = "LoginService",
targetNamespace = "http://LoginService")
public interface LoginService {
@WebMethod
@WebResult(name = "LoginResult")
LoginResult login(@WebParam(name = "username") String username, @WebParam(name = "password") String password
, @WebParam(name = "hostAddress", header = true) String hostAddress, @WebParam(name = "macAddress", header = true) String macAddress
);
}
从@WebParam的源码可以看出,header默认为false,false是接收body中的key-value,true是接收header中的key-value
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER})
public @interface WebParam {
String name() default "";
String partName() default "";
String targetNamespace() default "";
WebParam.Mode mode() default WebParam.Mode.IN;
boolean header() default false;
public static enum Mode {
IN,
OUT,
INOUT;
private Mode() {
}
}
}