使用GlassFish v3,EJB和SOAPUI [英] Using GlassFish v3, EJB and SOAPUI
问题描述
我想创建一个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()){
抛出新的CustomException(电子邮件不能为空) ;
}
if(password == null || password.isEmpty()){
抛出新的CustomException(密码不能为空);
}
返回新的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(){
returnAuthInfo [auth =+ auth +,token =+ token +] ;
}
}
类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。 p>
我将WSDL放在SOAPUI中,得到这个生成的请求:
; soapenv:Envelope xmlns:soapenv =http://schemas.xmlsoap.org/soap/envelope/xmlns:pac =http:// packageTest />
< soapenv:标题/>
< soapenv:Body>
< pac:login>
<! - 可选: - >
< email>电子邮件< / email>
<! - 可选: - >
< 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:信封>
请问有什么问题吗?我怀疑我发送的请求有问题,但注释可能有问题,因为这是我第一次尝试EJB Web服务。
我会期待一个包含由MyLogin类中的Web方法登录返回的字符串auth和token的答案。
没有看到 @WebParam
@WebMethod
public AuthInfo登录(@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;
}
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.
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;
}
这篇关于使用GlassFish v3,EJB和SOAPUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!