简体   繁体   中英

I am trying to consume a java web service from .NET. i am not getting proper output

code:

public static void main(String[] args) {
    try {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter the BookName:");
        String bookName = br.readLine();
        System.out.println("Book Id is: " +bookInfo(bookName).getBookAvail().get(0).getBookID().toString());
        System.out.println("Book Name is: " +bookInfo(bookName).getBookAvail().get(0).getBookName().toString());
    } catch (IOException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }
     try {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter the StudentID:");
        String studentID = br.readLine();
        System.out.println("Student Information is: " +studentInfo(studentID));
    } catch (IOException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }
}

private static ArrayOfBookAvail bookInfo(java.lang.String bookName) {
    org.tempuri.BookService service = new org.tempuri.BookService();
    org.tempuri.IBookService port = service.getBasicHttpBindingIBookService();
    return port.bookInfo(bookName);
}

private static ArrayOfStudInfo studentInfo(java.lang.String studentID) {
    org.tempuri.BookService service = new org.tempuri.BookService();
    org.tempuri.IBookService port = service.getBasicHttpBindingIBookService();
    return port.studentInfo(studentID);
}

}

and the output which i am getting is:

Enter the BookName:
Core Servlets and JavaServer Pages, Volume 2:Advanced Technologies
Book Id is: javax.xml.bind.JAXBElement@196da649
Enter the StudentID:
    13MCAL058
Student Information is: org.datacontract.schemas._2004._07.lms.ArrayOfStudInfo@2b08bc5a

please help to get the proper output. Thank you in advance.

Book Id is: javax.xml.bind.JAXBElement@196da649

Looks like bookInfo(bookName).getBookAvail().get(0).getBookID() returns a JAXBElement . You can just call getValue() instead of toString() , to get the value. When you call toString() , if the class hasn't overriden toString() of the inherited Object.class , then the Object#toString() will be called, which is the memory location of the object.

bookInfo(bookName).getBookAvail().get(0).getBookID().getValue();

Assuming the value is just a primitive or String, then the output should be fine.


Student Information is: 
             org.datacontract.schemas._2004._07.lms.ArrayOfStudInfo@2b08bc5a

I don't know what the ArrayOfStudInfo class is. But it seems to be the same issue, where Object#toString() is being called to get the String representation. Without knowing more about the ArrayOfStudInfo class, assuming it's your class, you could just override toString() in that class, to display what you want to be the String representation. For example

class ArrayOfStudInfo {
    StudentInfo[] studentInfoArray;

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        for (StudentInfo info : studentInfoArray) {
            builder.append(info.getFirstName())
                   .append(" ")
                   .append(info.getLastName())
                   .append("\n");
        }
        return builder.toString();
    }
}

Now I have no idea what the class consists of, I'm just throwing an idea out there. If you can't alter the class, then you can always just create a string by extracting the values from the object returned.


The point from this is that when you try and put an non-primitive object in a print statement, it needs to have a String representation, which is represented by the toString() method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM