5

I'm trying to create a simple Restful hello world api through intellij. I have created a maven project with the maven-archetype-quickstart and then in pom.xml I have added

<dependency>
  <groupId>org.jboss.resteasy</groupId>
  <artifactId>resteasy-jaxrs</artifactId>
  <version>2.3.7.Final</version>
  <scope>provided</scope>
</dependency>

Then in src->main->java->webservice I have two files: App.java

    package webservice;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
    }
}

and BookRestService.java

package webservice;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

@Path("/book")
public class BookRestService {
    @GET
    @Produces("text/plain")
    public String getBookTitle() {
        return "H2G2";
    }
}

I'm using jBoss-6.4. Jboss seems to start giving me the following message:

15:08:01,550 INFO  [org.jboss.ws.common.management] (MSC service thread 1-2) JBWS022052: Starting JBoss Web Services - Stack CXF Server 4.3.4.Final-redhat-1
15:08:01,824 INFO  [org.jboss.as.remoting] (MSC service thread 1-1) JBAS017100: Listening on 127.0.0.1:4447
15:08:01,824 INFO  [org.jboss.as.remoting] (MSC service thread 1-7) JBAS017100: Listening on 127.0.0.1:9999
15:08:01,827 INFO  [org.jboss.as.server.deployment.scanner] (MSC service thread 1-3) JBAS015012: Started FileSystemDeploymentService for directory /home/symeon/myprojects/applicationserver/jboss-eap-6.4-clean/standalone/deployments
15:08:02,113 INFO  [org.jboss.as] (Controller Boot Thread) JBAS015961: Http management interface listening on http://127.0.0.1:9990/management
15:08:02,114 INFO  [org.jboss.as] (Controller Boot Thread) JBAS015951: Admin console listening on http://127.0.0.1:9990
15:08:02,115 INFO  [org.jboss.as] (Controller Boot Thread) JBAS015874: JBoss EAP 6.4.0.GA (AS 7.5.0.Final-redhat-21) started in 5345ms - Started 153 of 191 services (57 services are lazy, passive or on-demand)
Connected to server
[2019-01-24 03:08:02,544] Artifact webservice: Artifact is being deployed, please wait...
15:08:02,721 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-6) JBAS015876: Starting deployment of "webservice" (runtime-name: "webservice.war")
15:08:03,236 INFO  [org.jboss.web] (ServerService Thread Pool -- 7) JBAS018210: Register web context: /webservice
15:08:03,400 INFO  [org.jboss.as.server] (management-handler-thread - 2) JBAS015859: Deployed "webservice" (runtime-name : "webservice.war")
[2019-01-24 03:08:03,428] Artifact webservice: Artifact is deployed successfully
[2019-01-24 03:08:03,428] Artifact webservice: Deploy took 884 milliseconds

What I would expect is when I open a browser and write http://localhost:8080/book to see H2G2 as answer. I have also tried http://localhost:8080/webservice/book since it mentions that the web context has been registered to /webservice but again without success. I get a 404 Not Found error. What I'm doing wrong?

5
  • 3
    try localhost:8080/webservice/book Commented Jan 24, 2019 at 13:15
  • Sorry...I didn't mention it. I have also tried localhost:8080/webservice/book due to the reason you mentioned, but I still get a 404 error Commented Jan 24, 2019 at 13:20
  • In resteasy you need to register your serive class or tell the resteasy to look in some root packages for service class. By default resteasy won't scan hole project for servicce class. Commented Jan 24, 2019 at 13:24
  • Thanks, but how is it done this? Commented Jan 24, 2019 at 13:25
  • can you show your web.xml file? try adding <context-param> <param-name>resteasy.scan</param-name> <param-value>true</param-value> </context-param> to your web.xml file. Commented Jan 24, 2019 at 13:31

3 Answers 3

1

In your App.java you need to define the ApplicationPath. Something like below.

import java.util.HashSet;
import java.util.Set;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("/restRoot")
public class App extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        HashSet<Class<?>> classes = new HashSet<Class<?>>();
        classes.add(BookRestService.class);

        return classes;
    }
}

After this you REST url will become

http://localhost:8080/webservice/restRoot/book
Sign up to request clarification or add additional context in comments.

1 Comment

It's not necessary the getClasses. And I just added @ApplicationPath("/"). So the response was on localhost:8080/webservice/book. Thanks
0
Register web context: /webservice

means that all the paths you've coded must be after /webservice. So instead of http://localhost:8080/book you should use http://localhost:8080/webservice/book

2 Comments

Sorry...I didn't mention it. I have also tried localhost:8080/webservice/book due to the reason you mentioned, but I still get a 404 error
@SymeonMattes what's your web.xml file?
0

By default resteasy won't scan your whole project for service class. You need to tell to resteasy to scan for it. Use below code in web.xml to enable the scan.

<context-param>
    <param-name>resteasy.scan</param-name>
    <param-value>true</param-value>
</context-param>

Source

2 Comments

In maven-archetype-quickstart it doesn't have a web.xml file
Rest services is a web application, Ideally you should create it as maven-archetype-webapp

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.