Rota PInk
Rota PInk

Reputation: 13

tomcat7 - How to deploy spring boot war file

I recreate a spring boot simple project "greetings", and when i run it On server, it work and i get JSON data in the browser like this:

localhost:9090/api/greetings

and it show:

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

and i generate successfully the war file via console :

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

and I have already set up tomcat7 on my ubuntu server 14.04 tls,

and throw the web manager I select the war file to deploy on the server:

but when tap the url to show all greetings for example, it doesn't show anything:

error : the requested resource type is not valid

so i check the error in the tomcat 7 catalina.out log:

INFO: Deploying web application archive /var/lib/tomcat7/webapps/demo-0.0.1-SNAPSHOT.war Jul 28, 2016 7:38:55 PM org.apache.catalina.loader.WebappClassLoader validateJarFile INFO: validateJarFile(/var/lib/tomcat7/webapps/demo-0.0.1-SNAPSHOT/WEB-INF/lib/javax.el-3.0.0.jar) - jar not loaded. See Servlet Spec 3.0, section 10.7.2. Offending class: javax/el/Expression.class Jul 28, 2016 7:38:55 PM org.apache.catalina.loader.WebappClassLoader validateJarFile INFO: validateJarFile(/var/lib/tomcat7/webapps/demo-0.0.1-SNAPSHOT/WEB-INF/lib/javax.servlet-api-3.1.0.jar) - jar not loaded. See Servlet Spec 3.0, section 10.7.2. Offending class: javax/servlet/Servlet.class Jul 28, 2016 7:38:55 PM org.apache.catalina.loader.WebappClassLoader validateJarFile INFO: validateJarFile(/var/lib/tomcat7/webapps/demo-0.0.1-SNAPSHOT/WEB-INF/lib/tomcat-embed-el-8.0.36.jar) - jar not loaded. See Servlet Spec 3.0, section 10.7.2. Offending class: javax/el/Expression.class

But i did't understand what's the issue exactly,

this is my project structure :

project structure

and this is my java classes:

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);
    }

}

and this is the pom file:

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

Upvotes: 0

Views: 723

Answers (1)

KubenT
KubenT

Reputation: 74

It is possible that you did not set your context path on application.properties and therefore Tomcat is using the default path of the application name.

Add server.contextPath=/ to the properties file to set the context path for the embedded tomcat and for the external tomcat add the context.xml with the below to your project to set the context path.

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

Upvotes: 0

Related Questions