线程“main”中的异常com.sun.xml.internal.ws.spi.db.DatabindingException:com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException

时间:2017-02-07 19:37:38

标签: java web-services jaxb

当我尝试运行代码时出现此错误。错误:

Exception in thread "main" com.sun.xml.internal.ws.spi.db.DatabindingException: com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
java.util.List is an interface, and JAXB can't handle interfaces.
    this problem is related to the following location:
        at java.util.List

    at com.sun.xml.internal.ws.db.glassfish.JAXBRIContextFactory.newContext(Unknown Source)
    at com.sun.xml.internal.ws.spi.db.BindingContextFactory.create(Unknown Source)
    at com.sun.xml.internal.ws.model.AbstractSEIModelImpl$1.run(Unknown Source)
    at com.sun.xml.internal.ws.model.AbstractSEIModelImpl$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.xml.internal.ws.model.AbstractSEIModelImpl.createJAXBContext(Unknown Source)
    at com.sun.xml.internal.ws.model.AbstractSEIModelImpl.postProcess(Unknown Source)
    at com.sun.xml.internal.ws.model.RuntimeModeler.buildRuntimeModel(Unknown Source)
    at com.sun.xml.internal.ws.db.DatabindingImpl.<init>(Unknown Source)
    at com.sun.xml.internal.ws.db.DatabindingProviderImpl.create(Unknown Source)
    at com.sun.xml.internal.ws.db.DatabindingProviderImpl.create(Unknown Source)
    at com.sun.xml.internal.ws.db.DatabindingFactoryImpl.createRuntime(Unknown Source)
    at com.sun.xml.internal.ws.server.EndpointFactory.createSEIModel(Unknown Source)
    at com.sun.xml.internal.ws.server.EndpointFactory.create(Unknown Source)
    at com.sun.xml.internal.ws.server.EndpointFactory.createEndpoint(Unknown Source)
    at com.sun.xml.internal.ws.api.server.WSEndpoint.create(Unknown Source)
    at com.sun.xml.internal.ws.api.server.WSEndpoint.create(Unknown Source)
    at com.sun.xml.internal.ws.transport.http.server.EndpointImpl.createEndpoint(Unknown Source)
    at com.sun.xml.internal.ws.transport.http.server.EndpointImpl.publish(Unknown Source)
    at com.sun.xml.internal.ws.spi.ProviderImpl.createAndPublishEndpoint(Unknown Source)
    at javax.xml.ws.Endpoint.publish(Unknown Source)
    at com.bookstore.BookStorePublisher.main(BookStorePublisher.java:12)

Caused by: com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
java.util.List is an interface, and JAXB can't handle interfaces.
    this problem is related to the following location:
        at java.util.List

    at com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException$Builder.check(Unknown Source)
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(Unknown Source)
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(Unknown Source)
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(Unknown Source)
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(Unknown Source)
    at com.sun.xml.internal.bind.v2.ContextFactory.createContext(Unknown Source)
    at com.sun.xml.internal.bind.api.JAXBRIContext.newInstance(Unknown Source)
    at com.sun.xml.internal.bind.api.JAXBRIContext.newInstance(Unknown Source)
    at com.sun.xml.internal.ws.developer.JAXBContextFactory$1.createJAXBContext(Unknown Source)

代码如下:

BookStoreServer.java

package com.bookstore;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

@WebService
@SOAPBinding(style = Style.RPC)
public interface BookStoreServer {

    @WebMethod
    HashMap<String,String> getBookDetails(String bookName);

    @WebMethod
    List<String> getAllBooks();
}

BookStoreImpl.java

package com.bookstore;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.jws.WebService;

import com.bookstore.BookStoreServer;

@WebService(endpointInterface = "com.bookstore.BookStoreServer")
public class BookStoreImpl implements BookStoreServer {

    public HashMap<String,String> getBookDetails(String bookName)
    {
        HashMap<String,String> hMap = new HashMap<String,String>();
        return hMap;
    }

    public ArrayList<String> setValuesToList(){
        ArrayList<String> strList = new ArrayList<String>();
        strList.add("testVal 1");
        strList.add("testVal 2");
        return strList;
    }
    public List<String> getAllBooks()
    {
        List<String> alist = new ArrayList<String>();
        BookStoreImpl impl = new BookStoreImpl();
        alist = impl.setValuesToList();
        return alist;
    }
}

BookStorePublisher.java

package com.bookstore;

import javax.xml.ws.Endpoint;

import com.bookstore.BookStoreImpl;

public class BookStorePublisher {

    public static void main(String[] args) {

        System.out.println("Beginning to publish HelloWorldService now ");
        Endpoint.publish("http://127.0.0.1:8930`enter code here`/ws", new BookStoreImpl());
        System.out.println("Done publishing");
    }

}

BookStoreClient.java

package com.bookstore;

import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;

public class BookStoreClient {

    public static void main(String[] args) {

        try {

            URL url = new URL("http://127.0.0.1:8930/ws?wsdl");
            QName qname = new QName("http://bookstore.com/",
                    "BookStoreImplService");

            Service service = Service.create(url, qname);

            BookStoreServer server = service.getPort(BookStoreServer.class);

               HashMap<String,String> m1 = new HashMap<String,String>(); 
                  m1.put("IAM", "A");
                  m1.put("COE", "B");
                  m1.put("ESRM", "C");
                  m1.put("TCS", "D");

            System.out.println("The book details");      
            //System.out.println(server.getBookdetails(m1));

            System.out.println("The book names");
        //  System.out.println(server.getAllBooks());
            List<String> bookList = server.getAllBooks();
            System.out.println("Executing for");
            for(String str:bookList){
                System.out.println("str: "+str);
            }

            Iterator<String> itr = bookList.iterator();
            while(itr.hasNext()){
                String book = itr.next();
            System.out.println(book);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

任何人都可以帮我解决这些错误。

1 个答案:

答案 0 :(得分:0)

在接口和实现类中将List更改为ArrayList,然后可以解决您的异常