【问题标题】:Java Spring Project on a Wildfly Server can't access RESTful ServicesWildfly 服务器上的 Java Spring 项目无法访问 RESTful 服务
【发布时间】:2017-01-12 11:54:20
【问题描述】:

我有一个 java 应用程序运行一个带有 RESRful 服务的 Spring 应用程序。

Windows

我有一个集成了 Eclipse 和 JBoss Wildfly 服务器的 Windows 环境。

可以成功访问 RESful 服务,例如通过http://localhost:8080/jbosswildfly/category/list.

OSX

我最近开始将它迁移到 OSX 环境。我安装了 Eclipse 和 Wildfly 服务器。

我可以访问显示服务器欢迎页面的http://localhost:8080/jbosswildfly/。但是,我无法通过例如访问 RESTful 服务。 http://localhost:8080/jbosswildfly/category/list (ERR_CONNECTION_REFUSED)

问题

如何让 RESTful 服务在 OSX 环境中运行?

更多信息

如您所见,在 Windows 环境中,jbosswildfly 服务器下有一个spring-web-4.3.1.RELEASE.jar,在 OSX 环境中不存在。

在 Windows 环境中,我从头开始创建 Java 项目,但在 OSX 环境中,我从 OpenShift Git 存储库导入现有项目。两种环境下的源代码完全相同。

该项目是使用 Maven 构建的,并且具有所有必需的依赖项。

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> 
        <version>1.3.5.RELEASE</version> </parent> -->

    <groupId>jbosswildfly</groupId>
    <artifactId>jbosswildfly</artifactId>
    <packaging>war</packaging>
    <version>1.0</version>
    <name>jbosswildfly</name>

    <repositories>
        <repository>
            <id>eap</id>
            <url>http://maven.repository.redhat.com/techpreview/all</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>eap</id>
            <url>http://maven.repository.redhat.com/techpreview/all</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <java.version>1.8</java.version>
        <maven.compiler.fork>true</maven.compiler.fork>
        <project.build.directory>${project.basedir}/target</project.build.directory>
        <springframework.version>4.3.1.RELEASE</springframework.version>
        <hibernate.version>5.2.1.Final</hibernate.version>
        <mysql.connector.version>5.1.31</mysql.connector.version>
        <junit.version>4.12</junit.version>
        <jackson.version>2.4.2</jackson.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.2-1003-jdbc4</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.connector.version}</version>
        </dependency>

        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <!-- <dependency> <groupId>org.springframework</groupId> <artifactId>spring-oxm</artifactId> 
            <version>${springframework.version}</version> </dependency> -->

        <!-- Spring boot -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>1.3.5.RELEASE</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>1.3.5.RELEASE</version>
            <scope>test</scope>
        </dependency>

        <!-- Spring test -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${springframework.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.2</version>
        </dependency>

        <!-- Hibernate -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${hibernate.version}</version>
        </dependency>

        <!-- <dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifactId> 
            <version>1.6.2</version> <type>jar</type> </dependency> -->

        <!-- jsr303 validation -->
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>1.1.0.Final</version>
        </dependency>

        <!-- Servlet+JSP+JSTL -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.0-b01</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.2-b02</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <!-- JSON -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-jsr310</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-xml</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20160810</version>
        </dependency>

        <!-- Sockets -->
        <dependency>
            <groupId>com.corundumstudio.socketio</groupId>
            <artifactId>netty-socketio</artifactId>
            <version>1.7.1</version>
        </dependency>

        <!-- Apache -->
        <dependency>
            <groupId>org.apache.opennlp</groupId>
            <artifactId>opennlp-tools</artifactId>
            <version>1.6.0</version>
        </dependency>

        <!-- Test -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>org.skyscreamer</groupId>
            <artifactId>jsonassert</artifactId>
            <version>1.3.0</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <profiles>
        <profile>
            <!-- When built in OpenShift the 'openshift' profile will be used when 
                invoking mvn. -->
            <!-- Use this profile for any OpenShift specific customization your app 
                will need. -->
            <!-- By default that is to put the resulting archive into the 'deployments' 
                folder. -->
            <!-- http://maven.apache.org/guides/mini/guide-building-for-different-environments.html -->
            <id>openshift</id>
            <build>
                <finalName>jbosswildfly</finalName>
                <plugins>

                    <plugin>
                        <groupId>org.wildfly.plugins</groupId>
                        <artifactId>wildfly-maven-plugin</artifactId>
                        <version>1.0.2.Final</version>
                        <configuration>
                            <failOnMissingWebXml>false</failOnMissingWebXml>
                            <outputDirectory>webapps</outputDirectory>
                            <warName>ROOT</warName>
                        </configuration>
                    </plugin>

                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-war-plugin</artifactId>
                        <version>2.3</version>
                        <configuration>
                            <failOnMissingWebXml>false</failOnMissingWebXml>
                            <outputDirectory>deployments</outputDirectory>
                            <warName>ROOT</warName>
                        </configuration>
                    </plugin>

                    <!-- <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-failsafe-plugin</artifactId> 
                        </plugin> -->
                    <!-- <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> 
                        </plugin> -->


                </plugins>
            </build>
        </profile>
    </profiles>
</project>

更新

我注意到的一件事是,在 Windows 环境中,target 文件夹包含许多生成的工件,但在 OSX 环境中,target 文件夹中有注释。

【问题讨论】:

  • 如果在 Eclipse 中右键单击项目并执行 Maven > Update project,然后重新发布到服务器会发生什么?
  • 感谢 Val Bonn,我试了一下,但是当我尝试访问 RESTful 服务时,我得到:404 - Not Found
  • 并且 jbosswildfly 下仍然没有出现 spring-web-4.3.1.jar ?你确定你在你的项目树中看到这个 jar,在 Java Resources > Libraries > Maven dependencies 下吗?你能在文件系统的“目标”目录下找到这个库吗?而且,在您的服务器上,您可以尝试“添加和删除”该应用程序吗?
  • spring-web-4.3.1.jar 仍然没有出现在 jbosswildfly 下。我也刚刚检查了target 文件夹,它在 OSX 环境中是空的。
  • 那说明你的项目没有建好。尝试运行 Maven 全新安装。

标签: java eclipse spring macos rest


【解决方案1】:

正如您在更新中注意到的那样,如果 target 为空,那是因为项目没有编译。

你必须在你的 OSX 上安装 maven。

【讨论】:

  • 我刚刚安装了maven。我从终端运行 mvn clean install 并成功构建。现在,从构建生成的 target 文件夹中也有工件。但是,我仍然无法访问 RESTful 服务 (not found)。此外,spring-web-4.3.1.RELEASE.jar 没有显示在 Eclipse 的服务器中的jbosswildfly 下。我也在 E​​clipse 中尝试过Maven -&gt; Update Project
猜你喜欢
  • 2018-12-12
  • 2019-07-09
  • 1970-01-01
  • 1970-01-01
  • 2016-11-18
  • 1970-01-01
  • 1970-01-01
  • 2011-11-01
  • 2019-07-04
相关资源
最近更新 更多