繁体   English   中英

tomcat7-如何部署Spring Boot War文件

[英]tomcat7 - How to deploy spring boot war file

我重新创建一个Spring Boot简单项目“ greetings”,当我在服务器上运行它时,它可以正常工作,并且在浏览器中获取JSON数据,如下所示:

本地主机:9090 / api / greetings

它显示:

[{“ id”:1,“ text”:“ Hello world1”},{“ id”:2,“ text”:“ Hello world2”}}]

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

... \\ workspace-sts-3.8.0.RELEASE \\ demo> MVN全新安装

并且我已经在Ubuntu Server 14.04 tls上设置了tomcat7,

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

但是,例如,点击网址以显示所有问候语时,它不显示任何内容:

错误:请求的资源类型无效

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

信息:部署Web应用程序存档/var/lib/tomcat7/webapps/demo-0.0.1-SNAPSHOT.war 2016年7月28日7:38:55 PM 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 Spec 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 Spec 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 Spec 3.0,第10.7.2节。 令人反感的类:javax / el / Expression.class

但我不明白到底是什么问题,

这是我的项目结构:

项目结构

这是我的java类:

1 /类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 /班问候

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 /类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>

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM