JBoss 7.1 上的 Rest 项目 运行 出错

Error with Rest Project running on JBoss 7.1

我在 JBoss 7.1 上创建了一个 REST 项目。 我给你需要的 类:

User.java

@XmlRootElement(name = "user")
public class User implements Serializable {
   private static final long serialVersionUID = 1L;
   private int id;
   private String name;
   private String profession;

   public User(){}

   public User(int id, String name, String profession){
        this.id = id;
    this.name = name;
    this.profession = profession;
   }

   public int getId() {
      return id;
   }
   @XmlElement
   public void setId(int id) {
      this.id = id;
   }
   public String getName() {
      return name;
   }
   @XmlElement
      public void setName(String name) {
      this.name = name;
   }
   public String getProfession() {
      return profession;
   }
   @XmlElement
   public void setProfession(String profession) {
      this.profession = profession;
   }
}

ListBean.java

@ManagedBean
@ViewScoped
public class ListBean {
    private static Client client;
    private static String REST_SERVICE_URL = "http://localhost:8080/UserManagement/rest/UserService/users";
    private static final String SUCCESS_RESULT="<result>success</result>";
    private static final String PASS = "pass";
    private static final String FAIL = "fail";
        private List<User> users;
    private transient DataModel<User> model;
    private User user;
    private int id;
    private String name;
    private String profession;

        private void init(){ 
           this.client = ClientBuilder.newClient();
        }

    @PostConstruct
    public void initialize()
    {
            ListBean tester = new ListBean();
        tester.init();
        GenericType<List<User>> list = new GenericType<List<User>>() {};
            users = client.target(REST_SERVICE_URL)
                  .request(MediaType.APPLICATION_XML)
                  .get(list);
            String result = PASS;
        if(users.isEmpty()){   
           result = FAIL;
        }
        List<User> lus = new ArrayList<User>();
        for(User u : users){   
           lus.add(u);
        }
    user = new User();
    }
 }

UserService.java

@Path("/UserService")
public class UserService
{   
   UserDao userDao = new UserDao();
   private static final String SUCCESS_RESULT="<result>success</result>";
   private static final String FAILURE_RESULT="<result>failure</result>";

   @GET
   @Path("/users")
   @Produces(MediaType.APPLICATION_XML)
   public List<User> getUsers(){
      return userDao.getAllUsers();
   }
 }

在运行之后,出现如下异常:

20:49:39,045 INFO  [org.jboss.as.server] (Controller Boot Thread) JBAS018559: Deployed "sqljdbc4.jar"
20:49:39,050 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-2) JBAS015876: Starting deployment of "UserManagement.war"
20:49:40,026 WARN  [org.jboss.as.server.deployment] (MSC service thread 1-2) JBAS015893: Encountered invalid class name 'com.sun.faces.vendor.Tomcat6InjectionProvider:org.apache.catalina.util.DefaultAnnotationProcessor' for service type 'com.sun.faces.spi.injectionprovider'
20:49:40,028 WARN  [org.jboss.as.server.deployment] (MSC service thread 1-2) JBAS015893: Encountered invalid class name 'com.sun.faces.vendor.Jetty6InjectionProvider:org.mortbay.jetty.plus.annotation.InjectionCollection' for service type 'com.sun.faces.spi.injectionprovider'
20:49:40,241 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-3) MSC00001: Failed to start service jboss.deployment.unit."UserManagement.war".POST_MODULE: org.jboss.msc.service.StartException in service jboss.deployment.unit."UserManagement.war".POST_MODULE: Failed to process phase POST_MODULE of deployment "UserManagement.war"
    at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:119) [jboss-as-server-7.1.1.Final.jar:7.1.1.Final]
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [rt.jar:1.7.0_55]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [rt.jar:1.7.0_55]
    at java.lang.Thread.run(Unknown Source) [rt.jar:1.7.0_55]
Caused by: org.jboss.as.server.deployment.DeploymentUnitProcessingException: JBAS011232: Only one JAX-RS Application Class allowed.  org.glassfish.jersey.server.ResourceConfig$WrappingResourceConfig org.glassfish.jersey.server.ResourceConfig$RuntimeConfig org.glassfish.jersey.server.ResourceConfig
    at org.jboss.as.jaxrs.deployment.JaxrsScanningProcessor.scan(JaxrsScanningProcessor.java:209)
    at org.jboss.as.jaxrs.deployment.JaxrsScanningProcessor.deploy(JaxrsScanningProcessor.java:105)
    at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:113) [jboss-as-server-7.1.1.Final.jar:7.1.1.Final]
    ... 5 more

这个项目在 Tomcat6 和 7 上运行良好,但是 JBoss7.1 有问题。

所以相关的异常是这样的:

JBAS011232: Only one JAX-RS Application Class allowed

感谢:

  • @eis 的帮助通过展示这个激励 link Deploying a Jersey webapp on Jboss AS 7
  • 线程Deploying a Jersey webapp on Jboss AS 7,此异常已解决。

然后,我遇到了这个问题:

java.lang.NoSuchMethodError: javax.ws.rs.core.MultivaluedMap.addAll(Ljava/lang/Object;[Ljava/lang/Object;)V

解决:

  • 我做了一些类implements Serializable.
  • 我在 META-INF 下添加了文件 jboss-deployment-structure.xml:
<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure>
    <deployment>
        <exclusions>
            <!-- Exclude RestEasy conflict -->
            <module name="javaee.api" />
            <module name="javax.ws.rs.api"/>
            <module name="org.jboss.resteasy.resteasy-jaxrs" />
        </exclusions>
    </deployment>
</jboss-deployment-structure>

因此,我的项目也能正常工作。