上下文路径中的应用程序无法启动Tomcat 7 Ubuntu

时间:2017-03-05 15:45:52

标签: java eclipse ubuntu tomcat

我正在尝试在运行Ubuntu 14.04的Tomcat 7中运行一个名为TestServer2的webapp,但是由于失败而失败 - 无法启动上下文路径/TestServer2-0.0.1-SNAPSHOT的应用程序。我在网上here上跟踪了一个示例,我在运行Eclipse的本地Tomcat服务器实例上进行了测试,它运行得很好但是当我将它部署到带有Tomcat 7的服务器Ubuntu 14.04时,它给了我这个错误。以下是我使用的代码:

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>TestServer2</display-name>


  <context-param>
        <param-name>resteasy.resources</param-name>
        <param-value>com.heyho.rest.JSONService</param-value>
    </context-param>

    <listener>
        <listener-class>
            org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
    </listener>
    <servlet>
        <servlet-name>resteasy-servlet</servlet-name>
        <servlet-class>
            org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>resteasy-servlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>



</web-app>

的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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>TestServer2</groupId>
    <artifactId>TestServer2</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <repositories>
        <repository>
            <id>JBoss repository</id>
            <url>https://repository.jboss.org/nexus/content/groups/public-jboss/</url>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.2</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jaxrs</artifactId>
            <version>2.2.1.GA</version>
        </dependency>

        <!-- <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-jaxb-provider</artifactId> 
            <version>2.2.0.GA</version> </dependency> -->

        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jackson-provider</artifactId>
            <version>2.2.1.GA</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.7</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.4</version>
        </dependency>

    </dependencies>

    <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <warSourceDirectory>WebContent</warSourceDirectory>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

JSONService.java

package com.heyho.rest;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;

@Path("/service")
public class JSONService {


    @POST
    @Path("/post")
    @Consumes("application/json")
    public Response createProductInJSON(UserInput input) {

        String result = "User input submitted : " + input;
        return Response.status(201).entity(result).build();

    }



}

UserInput.java

package com.heyho.rest;


public class UserInput {

    int counter;
    String attribute;
    String publicKey;

    public int getCounter() {
        return counter;
    }
    public void setCounter(int counter) {
        this.counter = counter;
    }
    public String getAttribute() {
        return attribute;
    }
    public void setAttribute(String attribute) {
        this.attribute = attribute;
    }
    public String getPublicKey() {
        return publicKey;
    }
    public void setPublicKey(String publicKey) {
        this.publicKey = publicKey;
    }

    @Override
    public String toString() {
        return "User Input = counter " + counter + " attribute = " + attribute + " public key = " + publicKey ;

    }
}

ClientPost.java

package com.heyho.client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;

import org.apache.commons.httpclient.protocol.Protocol;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;

import com.google.gson.Gson;

public class ClientPost {

    public static void main(String[] args) throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException {

        try {


            SSLContext sslContext = new SSLContextBuilder()
                      .loadTrustMaterial(null, (certificate, authType) -> true).build();

                    CloseableHttpClient client = HttpClients.custom()
                      .setSslcontext(sslContext)
                      .setSSLHostnameVerifier(new NoopHostnameVerifier())
                      .build();
            HttpPost postRequest = new HttpPost(
                "http://somewebsite.de:50064/TestServer2/service/post");

            Gson gson = new Gson();
            Map<String,Object> test = new HashMap<String,Object>();
            test.put("counter", 1000);
            test.put("attribute", "user attribute");
            test.put("publicKey", "user public key");
            String json = gson.toJson(test);
            StringEntity input = new StringEntity(json);
            input.setContentType("application/json");
            postRequest.setEntity(input);

            HttpResponse response = client.execute(postRequest);

            if (response.getStatusLine().getStatusCode() != 201) {
                throw new RuntimeException("Failed : HTTP error code : "
                    + response.getStatusLine().getStatusCode());
            }

            BufferedReader br = new BufferedReader(
                            new InputStreamReader((response.getEntity().getContent())));

            String output;
            System.out.println("Output from Server .... \n");
            while ((output = br.readLine()) != null) {
                System.out.println(output);
            }


          } catch (MalformedURLException e) {

            e.printStackTrace();

          } catch (IOException e) {

            e.printStackTrace();

          }

        }

    }

我的代码有问题吗?另外,我如何调试在服务器上运行的Tomcat 7,以便它可以给我一些日志或错误?

更新

我在日志中发现了这样的错误:

SEVERE: Exception sending context initialized event to listener instance of class org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
java.lang.UnsupportedClassVersionError: com/heyho/rest/JSONService : Unsupported major.minor version 52.0 (unable to load class com.heyho.rest.JSONService)

我不确定这里有什么问题。我在我的web.xml上声明了JSONService,但它仍然给我错误

1 个答案:

答案 0 :(得分:1)

“不支持的major.minor版本52.0”表示在尝试运行Java时,已为较新版本的Java编译了类git init git add . git commit -m "initial commit" git remote add origin https://github.com/user/repo.git 。将JVM升级到1.8,它应该可以工作。或者,您可以确保编译JVM版本的类。