Spring + JAX-WS : ‘xxx’ is an interface, and JAXB can’t handle interfaces

Problem

Integrate Spring + JAX-WS, see web service below :


package com.mkyong.user.ws;
//imports...

@WebService()
public class PGUserWS {

    //DI via Spring
    private UserBo userBo;

    public UserBo getUserBo() {
        return userBo;
    }
    
    public void setUserBo(UserBo userBo) {
        this.userBo = userBo;
    }

    @WebMethod(operationName = "addUser")
    public boolean addUser(@WebParam(name = "userId")
    String userId, @WebParam(name = "User")
    User user) throws SOAPException {

        userBo.addUser(userId, user);
        return true;
    }

}

The “userBo” is DI via Spring XML bean configuration. But, when the service is generating the service’s files (via wsgen) for deployment, it hits “JAXB can’t handle interfaces” error message, see below :


Caused by: java.security.PrivilegedActionException: 
	com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 
            2 counts of IllegalAnnotationExceptions
	com.mkyong.user.bo.UserBo is an interface, and JAXB can't handle interfaces.
        this problem is related to the following location:
                at com.mkyong.user.bo.UserBo
                at private com.mkyong.user.bo.UserBo com.mkyong.user.ws.jaxws.SetUserBo.arg0
                at com.mkyonguser.ws.jaxws.SetUserBo
	com.mkyong.user.bo.UserBo does not have a no-arg default constructor.
        this problem is related to the following location:
                at com.mkyong.user.bo.UserBo
                at private com.mkyong.user.bo.UserBo com.mkyong.user.ws.jaxws.SetUserBo.arg0
                at com.mkyong.user.ws.jaxws.SetUserBo

Solution

See this unofficial JAXB guide to learn how to map interface in JAXB.

However, your case is different, it just doesn’t make sense to generate web service method for “userBo“, which is only require to Di via Spring, not publish to client.

To stop “wsgen” to generate web method for “userBo“, just annotate it with “@WebMethod(exclude = true)“, see below :


package com.mkyong.user.ws;

//imports...

@WebService()
public class PGUserWS {

    private UserBo userBo;

    @WebMethod(exclude = true)
    public UserBo getUserBo() {
        return userBo;
    }

    @WebMethod(exclude = true)
    public void setUserBo(UserBo userBo) {
        this.userBo = userBo;
    }

    @WebMethod(operationName = "addUser")
    public boolean addUser(@WebParam(name = "userId")
    String userId, @WebParam(name = "User")
    User user) throws SOAPException {

        userBo.addUser(userId, user);
        return true;
    }

}

Now, “wsgen” will ignore the “userBo” getter and setter methods.

mkyong

Founder of Mkyong.com, passionate Java and open-source technologies. If you enjoy my tutorials, consider making a donation to these charities.

16 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
satish bhatia
6 years ago

does this solution apply if we use wsimport to create the classes? My wsimport generated application_soap_service class extends javax.xml.ws.Service. JAXB complains about the Executor and HandlerResolver interfaces, which I think it gets from the Service.getExecutor() and Service.getHandlerResolver() methods…

Marzie
11 years ago

Thanks a lot. It helped me a lot 🙂

Vikas
12 years ago

You rock!!!

Jeff
12 years ago

I’m starting to lose count of the number of times you’ve helped me. Thanks!

Gualtiero
12 years ago

Problem fixed. Thanks.

Sudhir kumar
13 years ago

I was facing the same issue. You gave the solution. Thank you very much Yong.

Gunjan Shah
13 years ago

Thanks You .. I was facing the same problem. It resolved my problem.

Flam182
13 years ago

Hello,

I add the annotation on my getter/setter :

  @WebMethod(exclude = true)

And i try to inject with spring :

 @Autowired
  private UserBo userBo;

But userBo is always null.
In applicationContext i have a “<context:component-scan" on my package.
I try to add this :

@WebService(serviceName = "PGUserWS ")
@Component("PGUserWS ")
Tania
11 years ago
Reply to  Flam182

i have the same issue, have you solved??

Adam
13 years ago

Thank you, just solved my problem!

vajpai
13 years ago

Excellent. It help lot.

Ben
14 years ago

Thanks! Exactly the problem I was having with a Spring-injected LdapTemplate. Added the “exclude” argument to the WebMethod annotation and my errors disappeared.

passer_by
15 years ago

thanks. it works!

Aditi
14 years ago
Reply to  passer_by

Thanks a lot…really helpfull..it worked

Paulo
11 years ago

You are the JBoss 🙂

Tati Danielle
7 years ago
Reply to  Paulo

You are the Wildfly!