问题
I would like to create a web service.
I am using Glassfish v3, an Eclipse Dynamic Web Project, and SOAPUI to test.
I have the following code in eclipse:
Class 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");
}
}
Class 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 generates the WSDL.
I place the WSDL in SOAPUI and get this generated request:
<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>
and get the response:
<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>
What is going wrong please? I suspect there is something wrong with the request I am sending, but there may be something wrong with the annotations as this is my first attempt at EJB web services.
I would expect an answer containing the Strings "auth" and "token" as returned by the web method login in class MyLogin.
回答1:
I don't see @WebParam
@WebMethod
public AuthInfo login(@WebParam(name = "email") String email, @WebParam(name = "password") String password) throws IOException, CustomException {
Try changing the signature like above and then try testing it using SOAPUI
Update
You also need to annotate your AuthInfo
class like,
@XmlAccessorType(value = XmlAccessType.NONE)
@Entity
public class AuthInfo{
@XmlElement
private String auth;
@XmlElement
private String token;
}
来源:https://stackoverflow.com/questions/8487348/using-glassfish-v3-ejb-and-soapui