尝试运行HelloWorld REST服务时获取404 [英] Getting 404 when trying to run HelloWorld REST service

查看:110
本文介绍了尝试运行HelloWorld REST服务时获取404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注 https://medium.com/@jamsesso/starting-out-with-jersey-apache-tomcat-using-intellij-6338d93ffd40#.9rmard5sl

我已经阅读了关于不解析com.sun.jersey.api.container.httpserver.HttpServerFactory的问题并且已经过去了(谢谢!)

I've read the question on not resolving com.sun.jersey.api.container.httpserver.HttpServerFactory and got past that (thanks!)

这里是我的代码:

package com.webbo.acronymserver;
import com.sun.jersey.api.container.httpserver.HttpServerFactory;
import com.sun.net.httpserver.HttpServer;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import java.io.IOException;

/**
 * Created by mark on 8/3/17.
 */
// The Java class will be hosted at the URI path "/helloworld"
@Path("/helloworld")
public class acronymServer {
    // The Java method will process HTTP GET requests
    @GET
    // The Java method will produce content identified by the MIME Media type "text/plain"
    @Produces("text/plain")
    public String getClichedMessage() {
        // Return some cliched textual content
        return "Hello World";
    }

    public static void main(String[] args) throws IOException {
        HttpServer server = HttpServerFactory.create("http://localhost:8080/");
        server.start();

    }
}

这是web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <servlet>
        <servlet-name>Example API</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>

        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>com.example.jersey</param-value>
        </init-param>

        <init-param>
            <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
            <param-value>true</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>Example API</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

我在IntelliJ IDEA Ultimate 2016.3中运行。可在 https://www.dropbox.com/s获取的压缩项目目录/r2gjwgzj7wbkb5k/acronymServer.zip?dl=0

I'm running in IntelliJ IDEA Ultimate 2016.3. Zipped project directory available at https://www.dropbox.com/s/r2gjwgzj7wbkb5k/acronymServer.zip?dl=0

我得到的只是404?非常感谢任何帮助。

All I get is a 404? Any help greatly appreciated.

推荐答案

您共享的项目有很多配置问题,仅举几例:

The project that you have shared has a lot of configuration issues, to name a few:


  • 来源位置与标准Maven布局不对应( / src / main / java ),因此根本不编译Java类

  • web应用程序位置也不正确(应该是 / src / main / webapp

  • 手动配置的依赖项( lib 目录)和通过Maven

  • 依赖项未添加到服务器上部署的工件

  • Maven包装未设置为 war

  • 重复和错误配置的Web构面

  • 启动URL设置为上下文的根,而不是 http:// localhost:8080 / helloworld servlet @Path

  • sources location doesn't correspond to the standard Maven layout (/src/main/java), therefore Java classes are not compiled at all
  • web app location also is not correct (should be /src/main/webapp)
  • dependencies configured manually (lib directory) and via Maven
  • dependencies not added to the artifact that is deployed on the server
  • Maven packaging is not set to war
  • duplicate and incorrectly configured web facets
  • startup URL is set to the root of the context instead of http://localhost:8080/helloworld servlet @Path

我已修复主要问题,以便它启动并显示 Hello World ,其余的将是你的家庭任务。

I've fixed the main problems so that it starts and displays Hello World, the rest will be your home task.

< img src =https://i.stack.imgur.com/m8K1V.pngalt =结构>

您可以下载修复项目此处。不要忘记在运行/调试配置中更改应用程序服务器,因为我使用不同的Tomcat版本进行了测试。

You can download the fixed project here. Don't forget to change the Application server in the run/debug configuration since I tested with a different Tomcat version.

这篇关于尝试运行HelloWorld REST服务时获取404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆