Spring Maven Web App - 提供范围

时间:2017-12-14 14:26:40

标签: java maven wildfly spring-web

我正在寻找使用Spring Framework 4.3.13部署maven Web应用程序的方法。当包含罐子时(编译范围),一切正常。我想使用(提供范围)排除jar文件。我有Wildfly 9作为我的应用程序服务器,我创建了spring框架模块。但当我删除罐子时,应用程序无法正常工作。没有错误! http://localhost:8080/springwebapp1应该显示欢迎信息,但事实并非如此。它显示Forbidden。 我一直在寻找一个如何做到这一点的例子,我还没有找到一个! 任何帮助将不胜感激!

这是我的配置: application root

的pom.xml

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.acs.springmvc</groupId>
  <artifactId>springwebapp1</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>springwebapp1 Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${springframework.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${springframework.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${springframework.version}</version>
            <scope>provided</scope>
        </dependency>    
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>        
  </dependencies>
  <build>
    <finalName>springwebapp1</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>                
                    <outputDirectory>${deploy.dir}</outputDirectory>
                </configuration>
            </plugin>
        </plugins> 
  </build>
  <properties>
    <deploy.dir>C:\javatools\AppServers\wildfly-9.0.1.Final\standalone\deployments</deploy.dir>
    <springframework.version>4.3.13.RELEASE</springframework.version>
  </properties>  
</project>

我在Wildfly中创建了弹簧模块,(org.springframework) 的 module.xml

<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.3" name="org.springframework">
  <resources>
    <resource-root path="spring-aop-4.3.13.RELEASE.jar"/>
    <resource-root path="spring-aspects-4.3.13.RELEASE.jar"/>
    <resource-root path="spring-beans-4.3.13.RELEASE.jar"/>
    <resource-root path="spring-context-4.3.13.RELEASE.jar"/>
    <resource-root path="spring-context-support-4.3.13.RELEASE.jar"/>
    <resource-root path="spring-core-4.3.13.RELEASE.jar"/>
    <resource-root path="spring-expression-4.3.13.RELEASE.jar"/>
    <resource-root path="spring-instrument-4.3.13.RELEASE.jar"/>
    <resource-root path="spring-instrument-tomcat-4.3.13.RELEASE.jar"/>
    <resource-root path="spring-jdbc-4.3.13.RELEASE.jar"/>
    <resource-root path="spring-jms-4.3.13.RELEASE.jar"/>
    <resource-root path="spring-messaging-4.3.13.RELEASE.jar"/>
    <resource-root path="spring-orm-4.3.13.RELEASE.jar"/>
    <resource-root path="spring-oxm-4.3.13.RELEASE.jar"/>
    <resource-root path="spring-test-4.3.13.RELEASE.jar"/>
    <resource-root path="spring-tx-4.3.13.RELEASE.jar"/>
    <resource-root path="spring-web-4.3.13.RELEASE.jar"/>
    <resource-root path="spring-webmvc-4.3.13.RELEASE.jar"/>
    <resource-root path="spring-webmvc-portlet-4.3.13.RELEASE.jar"/>
    <resource-root path="spring-websocket-4.3.13.RELEASE.jar"/>
  </resources>
  <dependencies>
    <module name="javax.api" export="true"/>
    <module name="javaee.api"/>
    <module name="org.apache.commons.logging"/>
    <module name="org.jboss.vfs"/>
    <module name="org.hibernate"/>
    <module name="javax.el.api" export="true"/>
    <module name="com.sun.xml.bind" export="true"/> 
  </dependencies>
</module>

我还尝试将 jboss-deployment-structure.xml 添加到war文件中。 (我放在WEB-INF中)它被包括在内。

<jboss-deployment-structure>
    <deployment>
        <dependencies>
            <module name="org.apache.commons.logging"/>
            <module name="org.springframework" slot="main" export="true" meta-inf="export"/>
        </dependencies>
    </deployment>
</jboss-deployment-structure>

这里是java类: ApplicationConfig.java

package com.acs.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.acs")
public class ApplicationConfig
{

}

ApplicationInitializer.java

package com.acs.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class ApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer
{
     @Override
        protected Class<?>[] getRootConfigClasses() {
            return new Class[] { ApplicationConfig.class };
        }

        @Override
        protected Class<?>[] getServletConfigClasses() {
            return null;
        }

        @Override
        protected String[] getServletMappings() {
            return new String[] { "/" };
        }
}

HelloRestController.java

package com.acs.controllers;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.acs.pojos.Message;

@RestController
public class HelloRestController {

    @RequestMapping("/")
    public String welcome() {//Welcome page, non-rest
        return "Welcome to RestTemplate Example.";
    }

    @RequestMapping("/hello/{player}")
    public Message message(@PathVariable String player) {//REST Endpoint.

        Message msg = new Message(player, "Hello " + player);
        return msg;
    }
}

0 个答案:

没有答案