0

I'm very new to Java, I'm attempting to create a very simple Rest API to return a string via a GET method. I've been through countless tutorials, but each time I get the above error using my project

url - http://localhost:8080/orchestrator/api/service/status

I'm using the ApplicationConfig.java - ApplicationPath file to denote the mapping:

@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(com.orchestrator.Services.class);
} }

Using Netbeans 12.6 with Tomcat via XAMPP v3.3.0; I have a web.xml but apparently I don't need it as I'm using the ApplicationConfig.java, but I'll post the one I made.

<servlet>
    <servlet-name>Jersey Web Application</servlet-name>
    <servlet-class>.com.sun.jerysey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>com.orchestrator.Services</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Jersey Web Application</servlet-name>
    <url-pattern>/api/*</url-pattern>
</servlet-mapping>

Services.java

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

@Context
private UriInfo context;

public Services() {
}

@Path("/status")
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getHtml() {
    //TODO return proper representation object
    return "connected";
}
}

Project Structure: image of folder structure

What am I missing to return the "connected" text, I've also tried using postman as well to return, same result.

Regards

  • Show me please, how ur web.xml looks like? – ProgrammerBoy Jan 06 '22 at 06:49
  • the web.xml is the second code block in the main post. Further exploring has shown that my project is deploying to tomcat -> work -> Catalina -> localhost. Shouldn't this be in webapps? – Locksley Jan 06 '22 at 18:57

1 Answers1

0

Looks like you're calling a wrong route.

Your applicationPath begins /api and not /orchestrator/api

You can try with this route http://localhost:8080/api/service/status

Hassan Liasu
  • 101
  • 3
  • 7
  • Unfortunately, this doesn't work using http://localhost:8080/api/service/status - browser returns - Message The requested resource [/api/service/status] is not available; The origin server did not find a current representation for the target resource or is not willing to disclose that one exists. – Locksley Jan 06 '22 at 06:07
  • What was returned when you try to reach http://localhost:8080 – Hassan Liasu Jan 06 '22 at 06:42
  • It returns the Tomcat main page; XAMPP also confirms it's running Tomcat on 8005, 8080 – Locksley Jan 06 '22 at 06:49
  • you can refer to this https://stackoverflow.com/questions/8520267/http-status-404-the-requested-resource-is-not-available – Hassan Liasu Jan 06 '22 at 07:37