Creating and Running RESTful Web Service on GlassFish

Chaklader Asfak Arefe :

I created a simple RESTful web service on the GlassFish server and run it according to this tutorial in the IntelliJ IDE. This runs fine based on the instruction provided. I have 2 additional questions,

a. The tutorial uses a service class provide below,

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

@Path("/helloworld")
public class HelloWorld {

    @GET
    @Produces("text/plain")
    public String getClichedMessage() {

        return "Hello World";
    }
}

I can access that from the URL provided,

http://localhost:8080/AppointmentManager_war_exploded/helloworld

Afterward, I add a new class in the same directory,

@Path("/")
public class App {

    @GET
    @Produces("text/plain")
    public String getMessage() {

        return "Hello, Berlin";
    }
}

I expected to see the message "Hello, Berlin" in the browser from the opening URL http://localhost:8080/AppointmentManager_war_exploded/, but, instead, I get the error provided,

HTTP Status 404 - Not Found
type Status report

messageNot Found

descriptionThe requested resource is not available.

GlassFish Server Open Source Edition 5.0

What is the issue here?

b. How do I change the part of URL AppointmentManager_war_exploded to something else, say, appointment etc? The artifact tab in the project setting is provided below,

enter image description here

I edited it, but, the change it not corresponded as expected.

I changed the project to maven build after the tutorial, but, the issue is not created for that. If someone interested, you can try too as it will take a minute to run.

Thank you.

Ruslan :

First

I expected to see the message "Hello, Berlin" in the browser from the opening URL http://localhost:8080/AppointmentManager_war_exploded/, but, instead, I get the error provided

In MyApplication class that provided by tutorial you should also add your new class:

@ApplicationPath("/")
public class MyApplication extends Application{
    @Override
    public Set<Class<?>> getClasses() {
        HashSet h = new HashSet<Class<?>>();
        h.add(HelloWorld.class);
        h.add(App.class);          // Add your new class here
        return h;
    }
}

Then you will be able to see expected page on http://localhost:8080/AppointmentManager_war_exploded/

Second

How do I change the part of URL AppointmentManager_war_exploded to something else, say, appointment etc?

URL contains name of your artifact AppointmentManager_war_exploded. This artifact automatically copied to glassfish application directory. You can check glassfish\domains\domain1\applications\__internal. Just change it just in project structure window here:

enter image description here

Update

Don't forget to change start URL in configuratin settings for app:

enter image description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=160313&siteId=1