Getting 404 when trying to run HelloWorld REST ser

2019-03-02 06:24发布

I'm following the example on https://medium.com/@jamsesso/starting-out-with-jersey-apache-tomcat-using-intellij-6338d93ffd40#.9rmard5sl

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

Here's my code:

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

    }
}

Here's the 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>

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

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

2条回答
对你真心纯属浪费
2楼-- · 2019-03-02 07:02

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

  • 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

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

structure

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.

查看更多
仙女界的扛把子
3楼-- · 2019-03-02 07:26

How are you accessing your api? It should be http://localhost:8080/<webapp context>/helloworld

查看更多
登录 后发表回答