Text HTML in Java Restful Web Services


Use JAR files which are listed below:

antlr-2.7.7.jar
asm-3.1.jar
commons-logging-1.1.3.jar
dom4j-1.6.1.jar
genson-1.3.jar
hibernate-commons-annotations-4.0.2.Final.jar
hibernate-core-4.2.5.Final.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
jackson-core-asl-1.9.2.jar
jackson-jaxrs-1.9.2.jar
jackson-mapper-asl-1.9.2.jar
jackson-xc-1.9.2.jar
javassist-3.15.0-GA.jar
javax.servlet.jsp.jstl-1.2.2.jar
javax.servlet.jsp.jstl-api-1.2.1.jar
jboss-logging-3.1.0.GA.jar
jboss-transaction-api_1.1_spec-1.0.1.Final.jar
jersey-client-1.18.jar
jersey-core-1.18.jar
jersey-json-1.19.3.jar
jersey-server-1.18.jar
jersey-servlet-1.18.jar
jettison-1.1.jar
jsr311-api-1.1.1.jar
mysql-connector-java-5.1.36.jar
taglibs-standard-impl-1.2.1.jar

Create Dynamic Web Project in Eclipse. Copy all jar files above to the lib directory in the project




This class is used to register a URL pattern in Jersey to intercept HTTP calls to the service. The marshaller, Jersey intercepts the path given in @ApplicationPath annotation such as – @ApplicationPath(“api”). This class extends javax.ws.rs.core.Application. All the RESTful web services and extensions needed are registered here.

ApplicationConfig.java

package ws;

import java.util.Set;
import javax.ws.rs.core.Application;

@javax.ws.rs.ApplicationPath("api")
public class ApplicationConfig extends Application {

	@Override
	public Set<Class<?>> getClasses() {
		Set<Class<?>> resources = new java.util.HashSet<>();
		addRestResourceClasses(resources);
		return resources;
	}

	private void addRestResourceClasses(Set<Class<?>> resources) {
		resources.add(controllers.DemoRestController.class);
	}

}

Create Restful Web Services provides text/html data for the client

package controllers;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("demo")
public class DemoRestController {

	@GET
	@Path("helloworld")
	@Produces({ MediaType.TEXT_HTML })
	public Response helloWorld() {
		try {
			return Response.ok("<b><i><u>Hello World</u></i></b>").build();
		} catch (Exception e) {
			return Response.status(Response.Status.BAD_REQUEST).build();
		}
	}

}

Access restful web services use the following url: http://localhost:9079/LearnJavaRestfulWebServices_Server/api/demo/helloworld

Output

Hello World




Create Java Project in Eclipse. Add all jar files above to the project

DemoRestClientModel class contain methods call restful web services

package models;

import javax.ws.rs.core.MediaType;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.DefaultClientConfig;

public class DemoRestClientModel {

	private String BASE_URL = "http://localhost:9079/LearnJavaRestfulWebServices_Server/api/";
	private WebResource webResource;
	private Client client;

	public DemoRestClientModel() {
		this.client = Client.create(new DefaultClientConfig());
		this.webResource = this.client.resource(BASE_URL).path("demo");
	}

	public ClientResponse helloWorld() {
		ClientResponse response = null;
		try {
			WebResource resource = this.webResource;
			response = resource.path("helloworld")
								.type(MediaType.TEXT_HTML)
								.get(ClientResponse.class);
		} catch (Exception e) {
			response = null;
		}
		return response;
	}

}
package demo;

import com.sun.jersey.api.client.ClientResponse;
import models.DemoRestClientModel;

public class Main {

	public static void main(String[] args) {

		DemoRestClientModel demoRestClientModel = new DemoRestClientModel();
		ClientResponse response = demoRestClientModel.helloWorld();
		if(response != null) {
			int statusCode = response.getStatus();
			System.out.println("Response Status: " + statusCode);
			String result = response.getEntity(String.class);
			System.out.println("Result: " + result);
		} else {
			System.err.println("Can not access helloWorld Web Method");
		}

	}

}




Response Status: 200
Result: <b><i><u>Hello World</u></i></b>