Guru Guru - 6 months ago 10
Java Question

Confusion in invoking maven project

I have a maven project which is running successfully on my system. This maven project takes any web URL as an input. Now I want to invoke this Maven project whenever my chrome extension is clicked. my chrome extension is capable of fetching the URL of the website.

So how can i do this? On searching google and many other resources, I came to know that I had to make a restful web service that can listen to the client side behavior.

But still I am not clear even how to do this? not even the initial steps are clear to me?

Any ideas would be greatly helpful to me!

I don't know anything about REST endpoint/postman, this was just the information, I gathered.

P.S
if the scenario, I described is not clear, please comment, I would try to add more details.

EDIT 1

Here's my source code for JS

document.addEventListener('DOMContentLoaded', function () {

chrome.tabs.query({ currentWindow: true, active: true }, function (tabs) {
alert(tabs[0].url)
var req = new XMLHttpRequest();
req.open("GET", "http://localhost:8080/SalesRESTservice/apicall/restcall2", true);
req.onload = onResponseReceived;
var par = 99.99;
req.send(par);

function onResponseReceived() {
alert("It worked.");
}
// document.getElementById('Current_url').value = tabs[0].url;
//console.log(tabs[0].url, tabs[0].title, tabs[0].incognito, tabs, this.bookmark_title);
});
});


And here's my JAVA endpoint code

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import org.json.JSONException;
import org.json.JSONObject;

@Path("/restcall2")
public class restcall2 {

@GET
@Produces("application/json")
public Response convertFtoC(Double param) throws JSONException {

JSONObject jsonObject = new JSONObject();
Double fahrenheit = param;
Double celsius;
celsius = (fahrenheit - 32)*5/9;
jsonObject.put("F Value", fahrenheit);
jsonObject.put("C Value", celsius);

String result = "@Produces(\"application/json\") Output: \n\nF to C Converter Output: \n\n" + jsonObject;
return Response.status(200).entity(result).build();
}

}


I tried sending value of fahrenheit to java func., but it gives error?

console Error

INFO: Server startup in 1052 ms
May 27, 2016 1:54:17 PM com.sun.jersey.spi.container.ContainerRequest getEntity
SEVERE: A message body reader for Java class java.lang.Double, and Java type class java.lang.Double, and MIME media type application/octet-stream was not found.
The registered message body readers compatible with the MIME media type are:
application/octet-stream ->
com.sun.jersey.core.impl.provider.entity.ByteArrayProvider
com.sun.jersey.core.impl.provider.entity.FileProvider
com.sun.jersey.core.impl.provider.entity.InputStreamProvider
com.sun.jersey.core.impl.provider.entity.DataSourceProvider
com.sun.jersey.core.impl.provider.entity.RenderedImageProvider
*/* ->
com.sun.jersey.core.impl.provider.entity.FormProvider
com.sun.jersey.core.impl.provider.entity.StringProvider
com.sun.jersey.core.impl.provider.entity.ByteArrayProvider
com.sun.jersey.core.impl.provider.entity.FileProvider
com.sun.jersey.core.impl.provider.entity.InputStreamProvider
com.sun.jersey.core.impl.provider.entity.DataSourceProvider
com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General
com.sun.jersey.core.impl.provider.entity.ReaderProvider
com.sun.jersey.core.impl.provider.entity.DocumentProvider
com.sun.jersey.core.impl.provider.entity.SourceProvider$StreamSourceReader
com.sun.jersey.core.impl.provider.entity.SourceProvider$SAXSourceReader
com.sun.jersey.core.impl.provider.entity.SourceProvider$DOMSourceReader
com.sun.jersey.json.impl.provider.entity.JSONJAXBElementProvider$General
com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General
com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General
com.sun.jersey.core.impl.provider.entity.XMLRootObjectProvider$General
com.sun.jersey.core.impl.provider.entity.EntityHolderReader
com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider$General
com.sun.jersey.json.impl.provider.entity.JSONListElementProvider$General

May 27, 2016 1:54:19 PM com.sun.jersey.spi.container.ContainerRequest getEntity
SEVERE: A message body reader for Java class java.lang.Double, and Java type class java.lang.Double, and MIME media type application/octet-stream was not found.
The registered message body readers compatible with the MIME media type are:
application/octet-stream ->
com.sun.jersey.core.impl.provider.entity.ByteArrayProvider
com.sun.jersey.core.impl.provider.entity.FileProvider
com.sun.jersey.core.impl.provider.entity.InputStreamProvider
com.sun.jersey.core.impl.provider.entity.DataSourceProvider
com.sun.jersey.core.impl.provider.entity.RenderedImageProvider
*/* ->
com.sun.jersey.core.impl.provider.entity.FormProvider
com.sun.jersey.core.impl.provider.entity.StringProvider
com.sun.jersey.core.impl.provider.entity.ByteArrayProvider
com.sun.jersey.core.impl.provider.entity.FileProvider
com.sun.jersey.core.impl.provider.entity.InputStreamProvider
com.sun.jersey.core.impl.provider.entity.DataSourceProvider
com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General
com.sun.jersey.core.impl.provider.entity.ReaderProvider
com.sun.jersey.core.impl.provider.entity.DocumentProvider
com.sun.jersey.core.impl.provider.entity.SourceProvider$StreamSourceReader
com.sun.jersey.core.impl.provider.entity.SourceProvider$SAXSourceReader
com.sun.jersey.core.impl.provider.entity.SourceProvider$DOMSourceReader
com.sun.jersey.json.impl.provider.entity.JSONJAXBElementProvider$General
com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General
com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General
com.sun.jersey.core.impl.provider.entity.XMLRootObjectProvider$General
com.sun.jersey.core.impl.provider.entity.EntityHolderReader
com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider$General
com.sun.jersey.json.impl.provider.entity.JSONListElementProvider$General

Answer

If you have a REST controller written, it should be up and listening to incoming requests. You need to provide those endpoint as request url and get information in your extension. You can use your browser directly or use extensions like POSTMAN to check and test your REST endpoints. And you can make an XMLHttpRequest from the chrome extension to your REST endpoints.

Example JS for invoking your REST API endpoint:

var req = new XMLHttpRequest();
req.open("GET", "http://api.myrestapi.com/services/rest/getEmployee", true);
req.onload = onResponseReceived;
req.send(null);

function onResponseReceived() {
    console.log("It worked.");
}