0

In many of the tutorial or example I saw. Most people put down the html code right in the java file. I was wondering if there is another way or better practice instead of writing something like this.

@Path("/someExample")
public class SomeExample{
@GET
    @Produces(MediaType.TEXT_HTML)
    public String sayHtmlHello() {
        return "<html> " + "<title>" + "Hello World RESTful Jersey"
                + "</title>" + "<body><h1>" + "Hello World RESTful Jersey"
                + "</body></h1>" + "</html> ";
}

I am new to the webservice and I was wondering instead of above can I do something like return from a html file itself?

4
  • take a look at this thread Commented May 18, 2017 at 1:11
  • suppose you can do like in this post by returning a FileInputStream Commented May 18, 2017 at 1:11
  • btw, what framework are you using or suppose to use? Commented May 18, 2017 at 1:34
  • REST doesn't have anything to do with HTML. Commented May 18, 2017 at 2:57

1 Answer 1

1

If you're looking to return dynamic HTML, you're probably better off using an MVC framework (like Spring MVC), rather than a REST framework. If you want to stick with using the same REST Framework you are using, if you are using Jersey, Jersey has MVC Support

The general idea of how MVC frameworks work, is by using templates and controllers to populate models used in the templates. In pseudo code, you might have something like

template (index.html)

<html>
   <body>
      <h1>Hello {{ name }}</h1>
   </body>
<html>

controller method

public Viewable index() {
    Map<String, String> model = new HashMap<>()
    model.put("name", "Peeskillet");
    return new Viewable("index", model);
}

This is example code that you might use with Jersey (and its MVC support), but using Spring MVC, the concept would still apply, just the classes used would be different.

The basic concept is that you fill the model inside the controller, and tell the framework which template should be used. The framework will take the template, and inject the variables wherever you request them, and then return the converted view to the client.

You should decide on which framework you want to use and read the documentation linked to above for more details. Spring MVC was made specifically as an MVC framework, where Jersey is a REST framework that added MVC support as an afterthought. So you will get more features with Spring MVC. But for basic MVC functionality, using Jersey would work.

As an aside, if you are already coming from an MVC framework (like Spring MVC) background, then you need to shift your thinking a little bit. With REST API (or web services as you call it), normally you won't be sending HTML page responses. Normally it will be a lighter weight data format like JSON. If you are creating a web application that interacts with the REST API, you would normally using AJAX (Javascript) to request the JSON, and it use the JSON data to update the DOM. That's generally how it works.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.