Création d'une api Restful simple en Java


J'essaie de créer une api Restful hello world simple via intellij. J'ai créé un projet maven avec le maven-archetype-quickstart puis dans pom.xml J'ai ajouté

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

, Puis dans src->général->java->webservice j'ai deux fichiers: App.java

    package webservice;

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

Et 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";
    }
}

J'utilise JBoss-6.4. Jboss semble commencer à me donner le message suivant:

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

Ce à quoi je m'attendrais, c'est quand j'ouvre un navigateur et que j'écris http://localhost:8080/book pour voir H2G2 comme réponse. J'ai également essayé http://localhost:8080/webservice/book puisqu'il mentionne que le contexte web a été enregistré dans /webservice mais encore une fois sans succès. J'obtiens une erreur 404 not Found. Ce que je fais mal?

Author: Symeon Mattes, 2019-01-24

3 answers

Votre App.java vous devez définir les ApplicationPath. Quelque chose comme ci-dessous.

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;
    }
}

Après cela, vous RESTEREZ URL deviendra

http://localhost:8080/webservice/restRoot/book
 1
Author: ScrapCode, 2019-01-24 14:01:14
Register web context: /webservice

Signifie que tous les chemins que vous avez codés doivent être after /webservice. Ainsi, au lieu de http://localhost:8080/book vous devez utiliser http://localhost:8080/webservice/book

 0
Author: Naya, 2019-01-24 13:18:52

Par défaut resteasy n'analysera pas l'ensemble de votre projet pour la classe de service. Vous devez dire à resteasy pour le rechercher. Utilisez le code ci-dessous dans web.xml pour activer l'analyse.

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

Source

 0
Author: Deepak Kumar, 2019-01-24 13:28:36