Tomcat Web Service mapping broken

时间:2017-04-24 17:25:33

标签: java tomcat

Lately I started working on a tomcat web service again.
I hadn't touched it in a couple of weeks, and when I went to try something I'd written, I received a 404 error.
Stumped by what was going on, I eventually decided to just write a new hello world service to see what was going on, but then that 404's too.

I don't recall any updates to Tomcat, or having tweaked any settings, yet I can no longer access any methods I write for anything and it's confusing the hell out of me.

For example, the below classes should mean that http://localhost:8080/HelloWorld/API/Test/ should return a "Hello World" message, however it 404s and I have no idea what's going on.

context.xml:

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

Application Config:

package main;

import java.util.Set;
import javax.ws.rs.core.Application;

@javax.ws.rs.ApplicationPath("API")
public class ApplicationConfig extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> resources = new java.util.HashSet<>();
        addRestResourceClasses(resources);
        return resources;
    }

    /**
     * Do not modify addRestResourceClasses() method.
     * It is automatically populated with
     * all resources defined in the project.
     * If required, comment out calling this method in getClasses().
     */
    private void addRestResourceClasses(Set<Class<?>> resources) {
        resources.add(main.HWTest.class);
    }

}

HWTest:

package main;

import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PUT;
import javax.ws.rs.core.MediaType;

@Path("/Test")
public class HWTest {

    @Context
    private UriInfo context;

    /**
     * Creates a new instance of HWTest
     */
    public HWTest() {
    }

    /**
     * Retrieves representation of an instance of main.HWTest
     * @return an instance of java.lang.String
     */
    @GET
    @Path("/")
    @Produces(MediaType.TEXT_PLAIN)
    public String getText() {
        //TODO return proper representation object
        return "Hello world";
    }
}

This definitely worked a few weeks ago, so I believe it might be something to do with a setting I accidentally tweaked, but I'm not sure as that wouldn't explain why new projects don't work either.

0 个答案:

没有答案