Tomcat服务器中的Spring启动war文件部署中的问题

时间:2017-11-14 20:11:11

标签: spring spring-boot deployment spring-cloud spring-cloud-config

我尝试在tomcat 8.5中部署war文件。但得到错误“有一个意外的错误(类型=未找到,状态= 404)。 没有可用的消息“

的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>edu.duton</groupId>
<artifactId>DutonFormProcessingBoot</artifactId>
<packaging>war</packaging>
<version>1</version>
<name>DutonFormProcessingBoot Maven Webapp</name>
<url>http://maven.apache.org</url>

<properties>
<java.version>1.8</java.version>
<start-class>edu.duton.test.Application</start-class>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

<!-- <tomcat.version>8.0.3</tomcat.version> -->

<!-- <jetty.version>9.1.0.v20131115</jetty.version>
<servlet-api.version>3.1.0</servlet-api.version> -->
</properties>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.8.RELEASE</version>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <!-- IN WAR PACKAGE WE SHOULD NOT EMBEDED SERVER -->
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <!-- IN WAR PACKAGE WE SHOULD PROVIDE SERVER -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope> 
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc7</artifactId>
        <version>12.1.0.1</version>
    </dependency>

    <!-- Below dependency is needed to process JSP pages -->
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <!-- IN WAR PACKAGE IT SHOULD BE PROVIDED -->
        <scope>provided</scope>
    </dependency>
     <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
     </dependency> 
     <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>bootstrap</artifactId>
        <version>3.3.7</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
</dependencies>

<!--Build configuration -->
<build>
    <!-- <sourceDirectory>src/test/java/edu/duton</sourceDirectory>  -->
    <plugins>
        <!-- using Java 8 -->
        <!-- <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin> -->
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <addResources>true</addResources>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <outputDirectory>target/foobar/</outputDirectory>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <mainClass>edu.duton.test.Application</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
    <finalName>DutonwarBoot</finalName>

   </build>
</project>

Application.java

package edu.duton.test;

import org.springframework.boot.Banner;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;


@SpringBootApplication(scanBasePackages={"edu.duton.config"})

public class Application extends SpringBootServletInitializer{  

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder 
application) {
    return application.sources(Application.class);
}

 public static void main(String[] args) {
      SpringApplication.run(Application.class, args);
 }
}

application.properties:

spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:xe
spring.datasource.username=system
spring.datasource.password=manager

#tomcat-connection settings
spring.datasource.tomcat.initialSize=20
spring.datasource.tomcat.max-active=25

debug=true

#Embedded Tomcat server 
server.port = 9991

spring.mvc.view.prefix= /WEB-INF/views/
spring.mvc.view.suffix= .jsp

management.security.enabled=false

CustomerController:

package edu.duton.controllers;
import java.util.Map;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import edu.duton.services.CustomerService;
import edu.duton.view.vo.Customer;

@Controller
@RequestMapping("/customers")
public class CustomerController{ //Generally controller class handles HTTP 
requests
@Autowired
private CustomerService custService;

@RequestMapping(value="/registration/form", method=RequestMethod.GET)
public String registerCustomer(Map<String, Object> m){ //endpoint
    m.put("customer", new Customer());
    return "NewCustomer";
}

@RequestMapping(value="/create", method=RequestMethod.POST)
public String insertCustomer(@Valid Customer customer, Errors errors) throws 
Exception { //endpoint
    if(errors.hasErrors()){ //In case of validation errors
        return "NewCustomer"; //redirected back to request page
    }

    Integer cid = custService.processCustomer(customer);
    customer.setCid(cid);

    return "success";
 }
}

CustomerService.java

package edu.duton.services;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import edu.duton.model.CustomerDao;
import edu.duton.view.vo.Customer;

@Service
public class CustomerService { //business class
@Autowired
private CustomerDao customerDao;

public int processCustomer(Customer c){
    return customerDao.save(c);
 }
}

CustomerDao.java:

package edu.duton.model;

import edu.duton.view.vo.Customer;

public interface CustomerDao {
public int save(Customer e);
public void update(Customer e);
public void delete(int eno);
public Customer get(int eno);
}

CustomerDaoImpl.java:

package edu.duton.model;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import edu.duton.view.vo.Customer;
//CREATE TABLE CUSTOMER(CID NUMBER(3)PRIMARY KEY, CNAME VARCHAR2(100), EMAIL 
VARCHAR2(100), MOBILE VARCHAR2(20));
//CREATE SEQUENCE CUSTOMER_SEQ;
@Repository
public class CustomerDaoImpl implements CustomerDao {
@Autowired(required = true)
private HibernateTemplate hibernateTemplate;

public CustomerDaoImpl() {
}

@Override
@Transactional(readOnly = false)
public int save(Customer c) {
    return (Integer) hibernateTemplate.save(c);
}

@Override
public void delete(int eno) {
}

@Override
public Customer get(int eno) {
    return null;
}

@Override
public void update(Customer e) {
}
}

客户:

package edu.duton.view.vo;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

public class Customer {
private int cid;
@Size(min = 3, max = 20, message = "Customer Name must be between 3 and 20 
characters long.")
@Pattern(regexp = "^[a-zA-Z ]+$", message = "Customer Name must be 
Alphabetic")
private String cname;
@Pattern(regexp = "^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}$", 
message = "Invalid Email Format")
private String email;
@Pattern(regexp="^\\d{10}$", message="Mobile number must be 10-digits 
numeric number")
private String mobile;

public Customer() {
}

public int getCid() {
    return cid;
}

public void setCid(int cid) {
    this.cid = cid;
}

public String getCname() {
    return cname;
}

public void setCname(String cname) {
    this.cname = cname;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getMobile() {
    return mobile;
}

public void setMobile(String mobile) {
    this.mobile = mobile;
}
}

DutonRootConfig.java:

package edu.duton.config;

import java.util.Properties;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;

@SpringBootConfiguration
@ComponentScan(basePackages = { "edu.duton.model", "edu.duton.services" })
@EnableAutoConfiguration
public class DutonRootConfig {
@Bean
public LocalSessionFactoryBean sessionFactory(DataSource ds) {
    LocalSessionFactoryBean lsfb = new LocalSessionFactoryBean();
    lsfb.setDataSource(ds);

    Properties props = new Properties();
    props.put("hibernate.show_sql", "true");
    props.put("hibernate.format_sql", "true");
    props.put("hibernate.use_sql_comments", "true");
    props.put("hibernate.transaction.factory_class", 
"org.hibernate.transaction.JDBCTransactionFactory");

    lsfb.setHibernateProperties(props);
    lsfb.setMappingResources(new String[] { "Customer.hbm.xml" });

    return lsfb;
}

@Bean(autowire = Autowire.BY_TYPE)
public HibernateTemplate hibernateTemplate() {
    return new HibernateTemplate();
}
}

DutonWebAppInitializer.java:

package edu.duton.config;

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

public class DutonWebAppInitializer extends 
AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected String[] getServletMappings() {
    return new String[] { "/" };
}

@Override
protected Class<?>[] getServletConfigClasses() {
    return new Class<?>[] { DutonWebConfig.class };
}

@Override
protected Class<?>[] getRootConfigClasses() {
    return new Class<?>[] { DutonRootConfig.class };
 }
}

DutonWebConfig.java:

package edu.duton.config;

import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import 
org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@SpringBootConfiguration
//The below annotation should be commented if we want to get Spring Boot MVC 
features.
//@EnableWebMvc
@ComponentScan("edu.duton.controllers")
public class DutonWebConfig {
/*  @Bean
public ViewResolver viewResolver() {
    InternalResourceViewResolver resolver = new 
InternalResourceViewResolver();
    resolver.setPrefix("/WEB-INF/views/");
    resolver.setSuffix(".jsp");
    return resolver;
}
*/
}

端点是: http://localhost:9992/Dutonwarboot/customers/registration/form

请让我知道如何解决它。

0 个答案:

没有答案