使用 GlassFish v3、EJB 和 SOAPUI

发布于 2024-12-20 23:16:48 字数 2731 浏览 5 评论 0原文

我想创建一个网络服务。

我正在使用 Glassfish v3、Eclipse 动态 Web 项目和 SOAPUI 进行测试。

我在 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 生成 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”的答案。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心在旅行 2024-12-27 23:16:48

我没有看到 @WebParam

@WebMethod
public AuthInfo login(@WebParam(name = "email") String email, @WebParam(name = "password") String password) throws IOException, CustomException {   

尝试像上面那样更改签名,然后尝试使用 SOAPUI

Update

测试它您还需要注释您的 AuthInfo 类喜欢,

@XmlAccessorType(value = XmlAccessType.NONE)
@Entity
public class AuthInfo{
    @XmlElement
    private String auth;

    @XmlElement
    private String token;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文