Home > Net >  RESTful response is not displaying in Chrome after successful test
RESTful response is not displaying in Chrome after successful test

Time:12-16

I am working my way through the activities section of my course and have hit a bit of a roadblock. The object of the activity is to display a string of text using a restful service, using the NetBeans IDE.

When I ran the TEST RESTful web services option in Netbeans it worked:

Successful RESTful service test

However, when I run the program all I see in the browser is a blank page:

what is displayed when webservice runs

At first I thought I had coded it incorrectly so I redid the exercise, but still came out with the same result. After one final try I then opened the solution file and have gotten the code correct, but the solution code displays an output, yet mine still does not. Why is the browser not displaying the pathway to the string?

If I type the pathway directly into Chrome it displays exactly as it should do.

I then tried adding a redirect to the index.html file which achieved the desired outcome of the exercise, but I don't think that is in the spirit of the question:

Browser display after redirect

I'm sure there is there a "proper" way of doing this, but I can't work it out. Here is my code:

RestService.java

package restService;

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

/**
 * REST Web Service
 *
 * @author Matthew
 */
@Path("rest")
public class RestSevice {

    @Context
    private UriInfo context;

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

    /**
     * Retrieves representation of an instance of restService.RestSevice
     * @return an instance of java.lang.String
     */
    @GET
    @Path("/banner")
    @Produces(MediaType.TEXT_HTML)
    public String getHtml() {
        return "<HTML><body><h1>This is a RESTful response!</h1></<body></html>";
    }

    /**
     * PUT method for updating or creating an instance of RestSevice
     * @param content representation for the resource
     */
    @PUT
    @Consumes(javax.ws.rs.core.MediaType.TEXT_PLAIN)
    public void putText(String content) {
    }
}

index.html

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <title>RESTful service</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <div>
            <ul>
                <meta http-equiv="Refresh" content="0; url='http://localhost:8080/RESTservice/webresources/rest/banner'" />
            </ul>
        </div>
    </body>
</html>

CodePudding user response:

After relooking at my code and worked example, I then discovered the way I have coded above is a "proper" way of doing things.

the example had a clickable link that then displayed the pathway, resulting in the correct link.

The blank page I was struggling with, was just an empty link. (kinda feel a bit foolish) and in redirecting automatically. it solved the problem.

  • Related