tomcat7 - 如何部署spring boot war文件

时间:2016-07-28 19:06:06

标签: java web-services maven tomcat spring-boot

我重新创建一个spring boot简单项目" greetings",当我运行它在服务器上时,它工作,我在浏览器中获取JSON数据,如下所示:

本地主机:9090 / API /问候

它显示:

[{" id":1," text":" Hello world1"},{" id":2,&# 34;文字":" Hello world2"}]

我通过控制台成功生成war文件:

  

... \ workspace-sts-3.8.0.RELEASE \ demo> mvn clean install

我已经在我的ubuntu服务器上安装了tomcat7 14.04 tls,

并抛出Web管理器我选择要在服务器上部署的war文件:

但是当点按网址以显示所有问候语时,它不会显示任何内容:

error : the requested resource type is not valid

所以我检查tomcat 7 catalina.out日志中的错误:

  

信息:部署Web应用程序存档/var/lib/tomcat7/webapps/demo-0.0.1-SNAPSHOT.war   2016年7月28日下午7:38:55 org.apache.catalina.loader.WebappClassLoader validateJarFile   信息:validateJarFile(/var/lib/tomcat7/webapps/demo-0.0.1-SNAPSHOT/WEB-INF/lib/javax.el-3.0.0.jar) - jar未加载。请参见Servlet规范3.0,第10.7.2节。违规类:javax / el / Expression.class   2016年7月28日下午7:38:55 org.apache.catalina.loader.WebappClassLoader validateJarFile   信息:validateJarFile(/var/lib/tomcat7/webapps/demo-0.0.1-SNAPSHOT/WEB-INF/lib/javax.servlet-api-3.1.0.jar) - jar未加载。请参见Servlet规范3.0,第10.7.2节。违规类:javax / servlet / Servlet.class   2016年7月28日下午7:38:55 org.apache.catalina.loader.WebappClassLoader validateJarFile   信息:validateJarFile(/var/lib/tomcat7/webapps/demo-0.0.1-SNAPSHOT/WEB-INF/lib/tomcat-embed-el-8.0.36.jar) - 未加载jar。请参见Servlet规范3.0,第10.7.2节。违规类:javax / el / Expression.class

但我确实不明白究竟是什么问题,

这是我的项目结构:

project structure

这是我的java类:

1 / class DemoApplication

@SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer{

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
      @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
            return application.sources(DemoApplication.class);
        }
 }

2 / class Greeting

public class Greeting {

    private BigInteger id;
    private String text;

    public BigInteger getId() {
        return id;
    }
    public void setId(BigInteger id) {
        this.id = id;
    }
    public String getText() {
        return text;
    }
    public void setText(String text) {
        this.text = text;
    }

}

3 / class GreetingController

@RestController
public class GreetingController {

    private static BigInteger nextId;
    private static Map<BigInteger, Greeting> greetingMap;

    private static Greeting save(Greeting greeting){
        if(greetingMap == null){
            greetingMap  = new HashMap<BigInteger, Greeting>();
            nextId = BigInteger.ONE;
        }


        //if update....
        if(greeting.getId() != null){
            Greeting oldGreeting = greetingMap.get(greeting.getId());
            if(oldGreeting == null){
                return null;
            }
            greetingMap.remove(greeting.getId());
            greetingMap.put(greeting.getId(), greeting);

            return greeting;
        }

        // if create....
        greeting.setId(nextId);
        nextId = nextId.add(BigInteger.ONE);
        greetingMap.put(greeting.getId(), greeting);

        return greeting;
    }


    static {
        Greeting g1 = new Greeting();
        g1.setText("Hello word");
        save(g1);

        Greeting g2 = new Greeting();
        g2.setText("Hello samsa");
        save(g2);   
    }

    @RequestMapping(
            value="/api/greetings", 
            method=RequestMethod.GET, 
            produces=MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Collection<Greeting>> getGreetings(){

        Collection<Greeting> greetings = greetingMap.values();      
        return new ResponseEntity<Collection<Greeting>>(greetings, HttpStatus.OK);


    }

    @RequestMapping(
            value="/api/greetings/{id}", 
            method=RequestMethod.GET, 
            produces=MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Greeting> getGreeting(@PathVariable("id") BigInteger id){
        Greeting greeting = greetingMap.get(id);
        if(greeting == null){
            return new ResponseEntity<Greeting>(greeting, HttpStatus.NOT_FOUND);
        }

        return new ResponseEntity<Greeting>(greeting, HttpStatus.OK);

    }


    @RequestMapping(
            value="/api/greetings", 
            method=RequestMethod.POST, 
            consumes=MediaType.APPLICATION_JSON_VALUE,
            produces=MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Greeting> createGreeting(@RequestBody Greeting greeting){

        Greeting savedGreeting = save(greeting);
        return new ResponseEntity<Greeting>(savedGreeting, HttpStatus.CREATED);

    }



    @RequestMapping(
            value="/api/greetings/{id}", 
            method=RequestMethod.PUT, 
            consumes=MediaType.APPLICATION_JSON_VALUE,
            produces=MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Greeting> updateGreeting(@RequestBody Greeting greeting){

        Greeting updatedGreeting = save(greeting);
        if(updatedGreeting == null){
            return new ResponseEntity<Greeting>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return new ResponseEntity<Greeting>(updatedGreeting, HttpStatus.OK);

    }


    private static boolean delete(BigInteger id) {
          Greeting deletedGreeding = greetingMap.remove(id);
          if(deletedGreeding == null){
              return false;
          }
          return true;
    }

    @RequestMapping(
            value="/api/greetings/{id}", 
            method=RequestMethod.DELETE, 
            consumes=MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Greeting> deletedGreeting(@PathVariable("id") BigInteger id, @RequestBody Greeting greeting){

        boolean deleted = delete(id);
        if(!deleted){
            return new ResponseEntity<Greeting>(HttpStatus.INTERNAL_SERVER_ERROR);
        }

        return new ResponseEntity<Greeting>(HttpStatus.NO_CONTENT);
    }

}

这是pom文件:

<?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>

    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <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-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-undertow</artifactId>
</dependency>

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

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

1 个答案:

答案 0 :(得分:0)

您可能没有在application.properties上设置上下文路径,因此Tomcat正在使用应用程序名称的默认路径。

server.contextPath=/添加到属性文件以设置嵌入式tomcat的上下文路径,并为外部tomcat将 context.xml 以及以下内容添加到项目中以设置上下文路径。

<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/"/>