gpt4 book ai didi

eclipse - org.springframework.web.SpringServletContainerInitializer 无法转换为 javax.servlet.ServletContainerInitializer -- Eclipse Spring 项目

转载 作者:行者123 更新时间:2023-11-28 22:04:56 26 4
gpt4 key购买 nike

请让我先告诉你我打算做什么。我打算完全按照 Springframework 网站中提到的“构建 RESTful Web 服务”教程进行操作 http://spring.io/guides/gs/rest-service/

我的问题是设置环境,以便我可以启动第一个原型(prototype)。我依赖于 Windows 7 64 位机器上的 Eclipse IDE。该项目基于“gradle”构建文件。当我尝试使用 build.gradle 运行项目时出现错误。以下是我尝试运行该项目的方式。

在项目资源管理器窗口中,右键单击项目>Run As>Gradle build>tomcatrun

build.gradle文件如下:

apply plugin: 'war'
apply plugin: 'tomcat'
apply plugin: 'java'
apply plugin: 'propdeps'
apply plugin: 'propdeps-maven'
apply plugin: 'propdeps-idea'
apply plugin: 'propdeps-eclipse'
apply plugin: 'eclipse'
apply plugin: 'idea'

buildscript {
repositories {
mavenCentral()
maven {
url "http://download.java.net/maven/2"
}
maven { url 'http://repo.spring.io/plugins-release' }
}

dependencies {
classpath 'org.gradle.api.plugins:gradle-tomcat-plugin:0.9.8'
classpath 'org.springframework.build.gradle:propdeps-plugin:0.0.1'
}
}


repositories {
mavenCentral()
}

dependencies {
def tomcatVersion = '7.0.12'
tomcat "org.apache.tomcat.embed:tomcat-embed-core:${tomcatVersion}",
"org.apache.tomcat.embed:tomcat-embed-logging-juli:${tomcatVersion}"
tomcat("org.apache.tomcat.embed:tomcat-embed-jasper:${tomcatVersion}") {
exclude group: 'org.eclipse.jdt.core.compiler', module: 'ecj'
}

compile 'org.springframework:spring-core:3.2.3.RELEASE'
compile 'org.springframework:spring-webmvc:3.2.3.RELEASE'
compile 'com.jayway.jsonpath:json-path:0.8.1'

compile 'org.slf4j:slf4j-api:1.7.5'
runtime 'org.slf4j:slf4j-jdk14:1.7.5'
runtime 'com.fasterxml.jackson.core:jackson-core:2.2.2'
runtime 'com.fasterxml.jackson.core:jackson-databind:2.2.2'
runtime 'javax.xml.bind:jaxb-api:2.2.9'

provided 'javax.servlet:javax.servlet-api:3.0.1'

testCompile 'com.jayway.jsonpath:json-path-assert:0.8.1'
testCompile 'org.springframework:spring-test:3.2.3.RELEASE'
testCompile 'junit:junit:4.+'
testCompile "org.mockito:mockito-all:1.9.5"

}

task wrapper(type: Wrapper) {
gradleVersion = '1.6'
}

tomcatRunWar.contextPath = ''

我认为问题在于正确定义包的依赖关系。 'javax.servlet:javax.servlet-api:3.0.1'

网络上的其他一些引用资料提到在构建文件中提供“provided”属性。该关键字确实存在于文件中。我尝试在一些 gradle 文档中查找如何执行相同的操作(以防丢失某些内容),但我找不到问题所在。本教程假定您在 linux shell 中运行构建。我认为使用 eclipse IDE 会触发错误。任何帮助将不胜感激。这是 WebApplicationInitializer 文件:

// {!begin top}
package com.yummynoodlebar.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.filter.DelegatingFilterProxy;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.FilterRegistration;
import javax.servlet.ServletContext;
import javax.servlet.ServletRegistration;
import java.util.Set;

public class WebAppInitializer implements WebApplicationInitializer {

private static Logger LOG = LoggerFactory.getLogger(WebAppInitializer.class);
// {!end top}

// {!begin onStartup}
@Override
public void onStartup(ServletContext servletContext) {
WebApplicationContext rootContext = createRootContext(servletContext);

configureSpringMvc(servletContext, rootContext);
}
// {!end onStartup}

// {!begin createRootContext}
private WebApplicationContext createRootContext(ServletContext servletContext) {
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
rootContext.register(CoreConfig.class);
rootContext.refresh();

servletContext.addListener(new ContextLoaderListener(rootContext));
servletContext.setInitParameter("defaultHtmlEscape", "true");

return rootContext;
}
// {!end createRootContext}

// {!begin configureTop}
private void configureSpringMvc(ServletContext servletContext, WebApplicationContext rootContext) {
AnnotationConfigWebApplicationContext mvcContext = new AnnotationConfigWebApplicationContext();
mvcContext.register(MVCConfig.class);

mvcContext.setParent(rootContext);

// {!end configureTop}
// {!begin configureBottom}
ServletRegistration.Dynamic appServlet = servletContext.addServlet(
"webservice", new DispatcherServlet(mvcContext));
appServlet.setLoadOnStartup(1);
Set<String> mappingConflicts = appServlet.addMapping("/");

if (!mappingConflicts.isEmpty()) {
for (String s : mappingConflicts) {
LOG.error("Mapping conflict: " + s);
}
throw new IllegalStateException(
"'webservice' cannot be mapped to '/'");
}
// {!end configureBottom}
}
}

非常感谢!

最佳答案

我在 Tomcat 7(使用 Maven)中配置基于 Spring 的 Web 应用程序时遇到此错误。在我的 pom.xml 中存在以下依赖项:

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>

因此 javax.servlet jar 被包含在部署工件中。在为上述依赖项提供“提供”的范围后,我能够解决这个问题。希望这会有所帮助....

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>

关于eclipse - org.springframework.web.SpringServletContainerInitializer 无法转换为 javax.servlet.ServletContainerInitializer -- Eclipse Spring 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21708828/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com