晚上好,
我最近一直在学习弹簧靴,我正在尝试使用弹簧支架和弹簧靴来开发一个简单的休息应用。
所以我在WildFly应用服务器上部署应用程序,我正在使用编码的应用程序上下文配置。我的问题是应用程序正常启动,没有错误或错误,但在尝试调用服务时没有任何反应,似乎请求未映射到控制器。有人可以向我解释在这种情况下如何完成映射? 以下是一些代码的和平,似乎有助于解决问题:
的web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>Restfull application</display-name>
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
<!-- Configure ContextLoaderListener to use AnnotationConfigWebApplicationContext
instead of the default XmlWebApplicationContext -->
<context-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</context-param>
<!-- Configuration locations must consist of one or more comma- or space-delimited
fully-qualified @Configuration classes. Fully-qualified packages may also be
specified for component-scanning -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>configuration.ApplicationContext</param-value>
</context-param>
</web-app>
ApplicationContext.java:
@Configuration
@ComponentScan({"DAL","domains","services","controllers","configuration"})
@EnableTransactionManagement
@PropertySource(value = { "classpath:application.properties" })
public class ApplicationContext extends SpringBootServletInitializer {
@Autowired
Environment environment;
private static Class<ApplicationContext> applicationClass = ApplicationContext.class;
public static void main(String[] args) {
SpringApplication.run(applicationClass, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(applicationClass);
}
@Bean(name = "datasource")
public BasicDataSource dataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(environment.getProperty("jdbc.driverClassName"));
dataSource.setUrl(environment.getProperty("jdbc.url"));
dataSource.setUsername(environment.getProperty("jdbc.username"));
dataSource.setPassword(environment.getProperty("jdbc.password"));
dataSource.setValidationQuery(environment.getProperty("validationQuery"));
return dataSource;
}
@Bean
public LocalSessionFactoryBean sessionFactoryBean() {
LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();
sessionFactoryBean.setDataSource(this.dataSource());
sessionFactoryBean.setPackagesToScan("*");
Properties properties = new Properties();
properties.setProperty("hibernate.dialect",environment.getProperty("hibernate.dialect"));
properties.setProperty("hibernate.show_sql",environment.getProperty("hibernate.show_sql"));
properties.setProperty("hibernate.hbm2ddl.auto",environment.getProperty("hibernate.hbm2ddl.auto"));
sessionFactoryBean.setHibernateProperties(properties);
return sessionFactoryBean;
}
@Bean
public HibernateTransactionManager transactionManager() {
HibernateTransactionManager transactionManager = new HibernateTransactionManager();
transactionManager.setSessionFactory(this.sessionFactoryBean().getObject());
return transactionManager;
}
@Bean
public GenericRepositoryImpl personRepo() {
GenericRepositoryImpl<Person,Integer> personRepo = new GenericRepositoryImpl<Person,Integer>();
return personRepo;
}
@Bean
public PersonServiceImpl personService() {
return new PersonServiceImpl();
}
}
ContactsController.java:
@RestController
@RequestMapping("/listAll")
public class ContactsController {
@Autowired
private IPersonService personService;
@RequestMapping(method = RequestMethod.GET)
public List<Person> getAllContacts() {
System.out.println("******** I'am here *********");
List<Person> contacts = personService.findAllContacts();
return contacts;
}
}
的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>TP2ArchitecturesLogicielles</groupId>
<artifactId>Activite3RestfulWebService</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>Activite3RestfulWebService Maven Webapp</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
</parent>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>http://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>http://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>http://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>http://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
<properties>
<java.version>1.8</java.version>
<hibernate.version>4.1.0.Final</hibernate.version>
<spring.version>4.1.4.RELEASE</spring.version>
<mysql.version>5.1.5</mysql.version>
</properties>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Hibernate dependencies -->
<!-- Hibernate validator -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!-- Hibernate core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!-- Spring dependencies -->
<!-- Spring Core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Spring Context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Spring ORM -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Spring Web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Commons tools -->
<!-- Commons pool -->
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>1.6</version>
</dependency>
<!-- Commons DBCP -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<!-- MySql connector -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
提前感谢您的帮助,并且对于过于冗长感到抱歉。
答案 0 :(得分:1)
有一些事情是不正确的。
首先,您的web.xml
旁边不需要web.xml
,而不是3.0版本的Servlet API的2.5版本。这会禁用Servlet 3.0的ServletContainerInitializer
支持。这有效地禁用了ApplicationContext
课程的学习。相反,只需删除它。
接下来你的依赖项,Spring Boot已经对它们进行了自动管理,并确保你获得所需的框架版本。关于依赖关系,Spring Boot提供了所谓的启动项目(您使用它),它已经引入了不同的依赖关系,因此无需再次添加它们。此外,Spring Boot已经为不同的库设置了依赖项。
修复你的pom
<properties>
部分您应该拥有以下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>TP2ArchitecturesLogicielles</groupId>
<artifactId>Activite3RestfulWebService</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>Activite3RestfulWebService Maven Webapp</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Hibernate dependencies -->
<!-- Hibernate validator -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
<!-- Hibernate core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
</dependency>
<!-- Spring ORM -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
</dependency>
<!-- Commons DBCP -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
</dependency>
<!-- MySql connector -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
有很多内容不需要,Spring Boot已经管理版本和正确的依赖项。
现在最后一部分与你的ApplicationContext
课程有关,它不是一个完整的Spring Boot课程。没有启用自动配置。添加@EnableAutoConfiguration
或简单地将@Configuration
替换为@SpringBootApplication
。 Spring Boot会自动启用事务,因此您可以删除@EnableTransactionManagement
并且application.properties
也已加载,因此@PropertySource
也应该加载。
同样从@ComponentScan
判断ApplicationContext
是否在某个根包中。这也意味着也可以。只留下@SpringBootApplication
注释。
@SpringBootApplication public class ApplicationContext扩展了SpringBootServletInitializer {...}
还提示将来使用Spring Initializr来设置您的项目。只需点击几下,您就可以获得正确的pom和预先配置的Application
课程。