尝试在java Web服务中返回对象时发生错误

时间:2012-09-21 08:27:22

标签: java web-services glassfish netbeans-7

有人请帮我解决这个错误。我是java web服务的新手。我得到如下所述的错误。我正在使用GlassFish服务器和NetBeans IDE。

CalculatorWSApplication a = port.setinfo(name, age);
required: CalculatorWSApplication
found:    CalculatorWSApplication_Type
1 error

这是服务器程序

package org.me.calculator;

import com.sun.tools.xjc.api.S2JJAXBModel;
import javax.annotation.*;
import javax.annotation.PreDestroy;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.ejb.Stateless;

@WebService(serviceName = "CalculatorWSApplication")
@Stateless()
public class CalculatorWSApplication {
public String firstName;
public String secondName;

    /**
     * Web service operation
     */
    @WebMethod(operationName = "add")
    public int add(@WebParam(name = "i") int i, @WebParam(name = "j") int j) {
        //TODO write your implementation code here:
        int k = i + j;
        return k;
    }


    @WebMethod(operationName = "setinfo")
    public CalculatorWSApplication setinfo(@WebParam(name = "firstName") String firstName, @WebParam(name = "secondName") String secondName) {
        CalculatorWSApplication a = new CalculatorWSApplication();
        //TODO write your implementation code here:
        System.out.println("Called");
        a.firstName = firstName;
        a.secondName = secondName;
        System.out.println(a.firstName);
        System.out.println(a.secondName);
        return a;
    }

    @PostConstruct
    public void sayHello(){
        System.out.println("Hello");
    }

    @PreDestroy
    public void sayBye(){
        System.out.println("Bye");
    }
}

这是客户端程序 包含两个Web方法。 第一个是add,它返回整数 第二个是setFirstNameandLastName,它返回CalculatorWSApplication对象。

package calculatorws_client_application;

import org.me.calculator.CalculatorWSApplication;

public class CalculatorWS_Client_Application {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        try{
    int i = 3;
    int j = 4;
    int result = add(i, j);
    setFirstNameandLastName("Rajesh","Gubbianna");
    System.out.println("Result = " + result);

        } catch(Exception ex){
            System.out.println("Exception: " + ex);
            ex.printStackTrace();
        }
    }

    private static int add(int i, int j) {
        org.me.calculator.CalculatorWSApplication_Service service = new org.me.calculator.CalculatorWSApplication_Service();
        org.me.calculator.CalculatorWSApplication port = service.getCalculatorWSApplicationPort();
        return port.add(i, j);
    }

    private static void setFirstNameandLastName(String name, String age){
        org.me.calculator.CalculatorWSApplication_Service service = new org.me.calculator.CalculatorWSApplication_Service();
        org.me.calculator.CalculatorWSApplication port = service.getCalculatorWSApplicationPort();
        System.out.println(name + " " + age);
        CalculatorWSApplication a = port.setinfo(name, age);
    }
}

1 个答案:

答案 0 :(得分:0)

有效的端点实现类必须满足以下要求:

  • 必须携带javax.jws.WebService注释。
  • 它的任何方法都可以带有javax.jws.WebMethod注释。
  • 除了任何特定于服务的例外之外,它的所有方法都可能抛出java.rmi.RemoteException。
  • 所有方法参数和返回类型必须与JAXB 2.0 Java to XML Schema映射定义兼容。
  • 方法参数或返回值类型不得直接或间接实现java.rmi.Remote接口。

Java数据类型


boolean,byte,double,float,long,int,javax.activation.DataHandler,java.awt.Image,java.lang.Object,java.lang.String等

有关详细信息和完整的数据类型列表,请查看此链接

Click Here