2

I'm green hand in web-service. I wrote a generic class as a value holder like this:

public class SearchResult<T> {
    private List<T> resultSet;
}

Then I write a web-service method:

public SearchResult<Book> getSearchResult(){
    ...
}

When I was using maven-jaxws-plugin to generate client files, I found that the generic type information was gone. They looked like :

public class SearchResult {
   private List<Object> resultSet;
}

public SearchResult getSearchResult(){
    ...
}

My question here is, do the jaxws can keep such kind of generic type information? I tried List as return type, it does work. Thanks in advance for your help.

3 Answers 3

3

Not sure of any other possibilities, but we are using

public class Books extends SearchResult<Book> {

}

Works quite well (as long as you don't have too many different types).

1

If the client code is generated using anything but the source, it will likely not contain the generic information. Once code using generics is compiled, information about generics is lost due to type erasure.

1

Use @XmlTransient and @XmlElement

Make your generic result class abstract, and the resultList @XmlTransient and protected.

public abstract class SearchResult<T> {        

    protected List<T> content;

    @XmlTransient
    public abstract List<T> getContent();

    public abstract void setContent(List<T> content);
}

Then create a subclass, use the @Xmlelement on the getter :

public class BookResult extends SearchResult<Book> {

    private static final long serialVersionUID = 1L;

    @Override
    @XmlElement
    public List<Book> getContent() {
        return content;
    }

    @Override
    public void setContent(List<Book> content) {
        this.content = content;
    }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.