我想创建一个Web服务。
我正在使用Glassfish v3,Eclipse动态Web项目和SOAPUI进行测试。
我在eclipse中有以下代码:
MyLogin课程
package packageTest;
import java.io.IOException;
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService
public class MyLogin {
@WebMethod
public AuthInfo login(@WebParam(name = "email") String email,@WebParam(name = "password")String password) throws IOException, CustomException {
if(email == null || email.isEmpty()){
throw new CustomException("Email cannot be empty.");
}
if(password == null || password.isEmpty()){
throw new CustomException("Password cannot be empty.");
}
return new AuthInfo("auth","token");
}
}
类AuthInfo
package packageTest;
import javax.persistence.Entity;
@Entity
public class AuthInfo {
private String token;
private String auth;
public AuthInfo(){}
public AuthInfo(String auth,String token) {
super();
this.token = token;
this.auth = auth;
}
public String getToken() {
return token;
}
public String getAuth() {
return auth;
}
@Override
public String toString() {
return "AuthInfo [auth=" + auth + ", token=" + token + "]";
}
}
Class CustomException
package packageTest;
public class CustomException extends Exception {
/**
*
*/
private static final long serialVersionUID = 1L;
public CustomException() {
}
public CustomException(String msg) {
super(msg);
}
public CustomException(String msg,
Throwable cause){
super(msg,cause);
}
}
Glassfish生成WSDL。
我将WSDL放在SOAPUI中并获取生成的请求:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:pac="http://packageTest/">
<soapenv:Header/>
<soapenv:Body>
<pac:login>
<!--Optional:-->
<email>email</email>
<!--Optional:-->
<password>password</password>
</pac:login>
</soapenv:Body>
</soapenv:Envelope>
并得到回复:
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:loginResponse xmlns:ns2="http://packageTest/">
<return/>
</ns2:loginResponse>
</S:Body>
</S:Envelope>
出了什么问题?我怀疑我发送的请求有问题,但注释可能有问题,因为这是我第一次尝试EJB Web服务。
我希望在MyLogin类中通过Web方法登录返回包含字符串“auth”和“token”的答案。
答案 0 :(得分:2)
我没有看到@WebParam
@WebMethod
public AuthInfo login(@WebParam(name = "email") String email, @WebParam(name = "password") String password) throws IOException, CustomException {
尝试更改上面的签名,然后尝试使用SOAPUI
进行测试<强>更新强>
您还需要注释AuthInfo
类,
@XmlAccessorType(value = XmlAccessType.NONE)
@Entity
public class AuthInfo{
@XmlElement
private String auth;
@XmlElement
private String token;
}