在JBoss 7.1上运行Rest Project时出错

时间:2016-08-16 20:15:35

标签: java rest jersey jboss7.x

我在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,但JBoss 7.1存在问题。

1 个答案:

答案 0 :(得分:1)

所以相关的例外就是:

JBAS011232: Only one JAX-RS Application Class allowed

感谢:

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

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

解决它:

  • 我制作了一些课程implements Serializable
  • 我在jboss-deployment-structure.xml下添加了文件META-INF
<?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>

结果,我的项目也可以。