REST服务确实在jboss AS 7中加载

时间:2017-10-11 20:01:01

标签: java eclipse maven jboss

我试图使用JBOSS AS7制作一些REST服务但是当我尝试访问它们时,日志显示为404.
我一直在寻找解决方案,但没有找到解决方案 这些服务位于WAR项目中,该项目位于EAR文件中,以便在JBOSS中进行部署。

我使用CXF和CDI来提供服务 JBOSS的EasyRest版本为3.0.5。

这是WAR项目的pom.xml:

<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">


<modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>projectParent</groupId>
        <artifactId>projectParent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <groupId>projectParent</groupId>
    <artifactId>projectWARModule</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>projectWARModule Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <org.apache.cxf.version>3.0.0</org.apache.cxf.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax.enterprise</groupId>
            <artifactId>cdi-api</artifactId>
            <version>1.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-integration-cdi</artifactId>
            <version>${org.apache.cxf.version}</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.annotation/javax.annotation-api -->
        <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>javax.annotation-api</artifactId>
            <version>1.2</version>
        </dependency>

        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>${org.apache.cxf.version}</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-wsdl -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-wsdl</artifactId>
            <version>3.0.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-ws-policy -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-ws-policy</artifactId>
            <version>3.0.0</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.jaxrs</groupId>
            <artifactId>jackson-jaxrs-json-provider</artifactId>
            <version>2.2.3</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.2.3</version>
        </dependency>

        <dependency>
            <groupId>org.jboss.weld.servlet</groupId>
            <artifactId>weld-servlet</artifactId>
            <version>2.1.2.Final</version>
        </dependency>

        <dependency>
            <groupId>org.jboss.weld</groupId>
            <artifactId>weld-core</artifactId>
            <version>2.1.2.Final</version>
        </dependency>
        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>1</version>
        </dependency>
    </dependencies>

    <repositories>
        <repository>
            <id>maven2-repository.dev.java.net</id>
            <url>http://download.java.net/maven/2/</url>
        </repository>
        <repository>
            <id>jboss-public</id>
            <url>http://repository.jboss.org/nexus/content/groups/public/</url>
        </repository>
    </repositories>
    <build>
        <finalName>projectWAR</finalName>
    </build>
</project>

这是EAR包的pom.xml,它引用了一个JAR,因为在一个教程中,我发现使用maven制作EAR,它说要使用它,但没有任何东西:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>projectParent</groupId>
    <artifactId>projectParent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>projectEARModule</artifactId>
  <packaging>ear</packaging>
  <dependencies>
    <dependency>
        <groupId>projectParent</groupId>
        <artifactId>projectJARModule</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>projectParent</groupId>
        <artifactId>projectWARModule</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <type>war</type>
    </dependency>
  </dependencies>
  <build>
    <finalName>projectEar</finalName>
  </build>
</project>

此类名为PeopleRestService,并具有以下服务:

package com.example.rs;

import java.util.Collection;

import javax.inject.Inject;
import javax.ws.rs.DELETE;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

import com.example.model.Person;
import com.example.services.PeopleService;

@Path( "/people" )
public class PeopleRestService {


@Inject private PeopleService peopleService;

    @Produces( { MediaType.APPLICATION_JSON } )
    @GET
    public Collection< Person > getPeople( @QueryParam( "page") @DefaultValue( "1" ) final int page ) {
        return peopleService.getPeople( page, 5 );
    }

    @Produces( { MediaType.APPLICATION_JSON } )
    @Path( "/{email}" )
    @GET
    public Person getPerson( @PathParam( "email" ) final String email ) {
        return peopleService.getByEmail( email );
    }

    @Produces( { MediaType.APPLICATION_JSON  } )
    @POST
    public Response addPerson( @Context final UriInfo uriInfo,
            @FormParam( "email" ) final String email, 
            @FormParam( "firstName" ) final String firstName, 
            @FormParam( "lastName" ) final String lastName ) {

        final Person person = peopleService.addPerson( email, firstName, lastName );
        return Response.created( uriInfo.getRequestUriBuilder().path( email ).build() ).entity( person ).build();
    }

    @Produces( { MediaType.APPLICATION_JSON  } )
    @Path( "/{email}" )
    @PUT
    public Person updatePerson(         
            @PathParam( "email" ) final String email, 
            @FormParam( "firstName" ) final String firstName, 
            @FormParam( "lastName" )  final String lastName ) {

        final Person person = peopleService.getByEmail( email );

        if( firstName != null ) {
            person.setFirstName( firstName );
        }

        if( lastName != null ) {
            person.setLastName( lastName );
        }

        return person;              
    }

    @Path( "/{email}" )
    @DELETE
    public Response deletePerson( @PathParam( "email" ) final String email ) {
        peopleService.removePerson( email );
        return Response.ok().build();
    }
}

这被命名为JaxRsApiApplication:

package com.example.rs;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import javax.enterprise.inject.Produces;
import javax.inject.Inject;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;

@ApplicationPath( "api" )
public class JaxRsApiApplication extends Application {

}

这是beans.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
       http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
       bean-discovery-mode="all">
</beans>

这是web.xml:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <display-name>Archetype Created Web Application</display-name>

    <context-param>
        <param-name>resteasy.scan</param-name>
        <param-value>false</param-value>
    </context-param>
    <context-param>
        <param-name>resteasy.scan.providers</param-name>
        <param-value>false</param-value>
    </context-param>
    <context-param>
        <param-name>resteasy.scan.resources</param-name>
        <param-value>false</param-value>
    </context-param>

    <listener>
        <listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
    </listener>

    <servlet>
        <servlet-name>PeopleRestService</servlet-name>
        <servlet-class>org.apache.cxf.cdi.CXFCdiServlet</servlet-class>
        <init-param>
            <param-name>org.apache.cxf.cdi</param-name>
            <param-value>com.example.rs</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>PeopleRestService</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

</web-app>

当我尝试在tomcat中运行它完美时,但在JBOSS中只获得404 我认为这可能是图书馆的问题,但不知道特别是什么问题。

如果有人知道会发生什么,那将是一个很大的帮助。 非常感谢,如果我的英语不好,那就很抱歉。

祝你有个愉快的一天。

更新
正如@JanezKuhar所说,我重新计算了pom的进口量。现在依赖关系看起来像:

<dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>

<!--        <dependency> -->
<!--            <groupId>javax.enterprise</groupId> -->
<!--            <artifactId>cdi-api</artifactId> -->
<!--            <version>1.1</version> -->
<!--        </dependency> -->
<!--        <dependency> -->
<!--            <groupId>org.apache.cxf</groupId> -->
<!--            <artifactId>cxf-integration-cdi</artifactId> -->
<!--            <version>${org.apache.cxf.version}</version> -->
<!--        </dependency> -->
<!--        <dependency> -->
<!--            <groupId>org.apache.cxf</groupId> -->
<!--            <artifactId>cxf-rt-frontend-jaxrs</artifactId> -->
<!--            <version>${org.apache.cxf.version}</version> -->
<!--        </dependency> -->
<!--        <dependency> -->
<!--            <groupId>javax.annotation</groupId> -->
<!--            <artifactId>javax.annotation-api</artifactId> -->
<!--            <version>1.2</version> -->
<!--        </dependency> -->
<!--        <dependency> -->
<!--            <groupId>org.apache.cxf</groupId> -->
<!--            <artifactId>cxf-rt-transports-http</artifactId> -->
<!--            <version>${org.apache.cxf.version}</version> -->
<!--        </dependency> -->
<!--        <dependency> -->
<!--            <groupId>org.apache.cxf</groupId> -->
<!--            <artifactId>cxf-rt-wsdl</artifactId> -->
<!--            <version>3.0.0</version> -->
<!--        </dependency> -->
<!--        <dependency> -->
<!--            <groupId>org.apache.cxf</groupId> -->
<!--            <artifactId>cxf-rt-ws-policy</artifactId> -->
<!--            <version>3.0.0</version> -->
<!--        </dependency> -->

        <dependency>
            <groupId>com.fasterxml.jackson.jaxrs</groupId>
            <artifactId>jackson-jaxrs-json-provider</artifactId>
            <version>2.2.3</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.2.3</version>
        </dependency>

<!--        <dependency> -->
<!--            <groupId>org.jboss.weld.servlet</groupId> -->
<!--            <artifactId>weld-servlet</artifactId> -->
<!--            <version>2.1.2.Final</version> -->
<!--        </dependency> -->
<!--        <dependency> -->
<!--            <groupId>org.jboss.weld</groupId> -->
<!--            <artifactId>weld-core</artifactId> -->
<!--            <version>2.1.2.Final</version> -->
<!--        </dependency> -->
<!--        <dependency> -->
<!--            <groupId>javax.inject</groupId> -->
<!--            <artifactId>javax.inject</artifactId> -->
<!--            <version>1</version> -->
<!--        </dependency> -->
    </dependencies>

但现在它给了我以下错误:

引起:java.lang.ClassNotFoundException:org.apache.cxf.cdi.CXFCdiServlet来自[Module&#34; deployment.projectEar.ear.projectWARModule-0.0.1-SNAPSHOT.war:main&#34;来自服务模块加载器]

2 个答案:

答案 0 :(得分:0)

最终完成这项工作。

正如@JanezKuhar所说,我评论了pom中的所有额外库并添加了两个,现在看起来像这样:

<?xml version="1.0"?>
<project
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
    xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>projectParent</groupId>
        <artifactId>projectParent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <groupId>projectParent</groupId>
    <artifactId>projectWARModule</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>projectWARModule Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <org.apache.cxf.version>3.0.0</org.apache.cxf.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf</artifactId>
            <version>3.0.0</version>
            <type>pom</type>
            <scope>provided</scope> 
        </dependency>
        <dependency>
            <groupId>org.jboss.weld.servlet</groupId>
            <artifactId>weld-servlet</artifactId>
            <version>2.1.2.Final</version>
        </dependency>
    </dependencies>

    <repositories>
        <repository>
            <id>maven2-repository.dev.java.net</id>
            <url>http://download.java.net/maven/2/</url>
        </repository>
        <repository>
            <id>jboss-public</id>
            <url>http://repository.jboss.org/nexus/content/groups/public/</url>
        </repository>
    </repositories>

    <build>
        <finalName>projectWAR</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-ejb-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <ejbVersion>3.1</ejbVersion>
                    <archive>
                        <manifestEntries>
                            <Dependencies>org.apache.cxf.impl</Dependencies>
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

在web.xml中注释servlet部分并将restEasy scan设置为true:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <display-name>Archetype Created Web Application</display-name>

    <context-param>
        <param-name>resteasy.scan</param-name>
        <param-value>true</param-value>
    </context-param>
    <context-param>
        <param-name>resteasy.scan.providers</param-name>
        <param-value>true</param-value>
    </context-param>
    <context-param>
        <param-name>resteasy.scan.resources</param-name>
        <param-value>true</param-value>
    </context-param>

    <listener>
        <listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
    </listener>

</web-app>

现在它可以工作,我可以访问jboss中部署的REST服务。

感谢您提供的帮助。

答案 1 :(得分:0)

你的jboss-app.xml在哪里? maven是否创造了它? 在jboss-app.xml中,您(或maven)应该创建应用程序的上下文根。

  

jboss-app.xml :每个EAR应用程序都应为类加载器指定唯一的字符串名称。它来自JBoss doc。 link