我有一个在我的系统上成功运行的maven项目。此maven项目将任何Web URL作为输入。现在,我想在点击我的chrome扩展时调用这个Maven项目。我的chrome扩展程序能够获取网站的URL。
那我怎么能这样做?在搜索谷歌和许多其他资源时,我发现我必须制作一个可以听取客户端行为的宁静网络服务。
但是我还不清楚怎么做呢?甚至最初的步骤对我来说都不清楚?
任何想法对我都有帮助!
我对REST端点/邮递员一无所知,这只是我收集的信息。
P.S
如果我描述的情景不清楚,请发表评论,我会尝试添加更多细节。
编辑1
这是JS的源代码
document.addEventListener('DOMContentLoaded', function () {
chrome.tabs.query({ currentWindow: true, active: true }, function (tabs) {
alert(tabs[0].url)
var req = new XMLHttpRequest();
req.open("POST", "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);
});
});
这是我的JAVA端点代码
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 {
@POST
@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();
}
}
我尝试将fahrenheit的值发送到java func。但是它会出错?
控制台错误
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
答案 0 :(得分:1)
从https://jersey.java.net/开始 Jersey RESTful Web Services框架是开源的。 你会从那里得到一个简短的想法。
此外,您可以在此处获取基本的平针织网页应用程序 https://github.com/Vijendra07Kulhade/jersey-quickstart
只需克隆它并运行maven install。
要运行maven项目,请在pom.xml中添加以下插件
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
</plugin>
您可以从上面的github repo中获取参考。
运行命令mvn tomcat7:run
它将在tomcat中运行您的项目。它还将告诉它部署的上下文。类似下面的东西。你可以从邮递员那里得到这个。
localhost:8080/someContext/
答案 1 :(得分:1)
如果您编写了一个REST控制器,它应该启动并监听传入的请求。您需要将这些端点作为请求URL提供,并在您的扩展中获取信息。您可以直接使用浏览器或使用POSTMAN等扩展来检查和测试REST端点。您可以从chrome扩展程序向REST端点创建XMLHttpRequest。
用于调用REST API端点的JS示例:
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.");
}
如果你需要使用POST方法发送参数,试试这个,
var http = new XMLHttpRequest();
var someValue = "99.99";
var url = "http://localhost:8080/SalesRESTservice/apicall/restcall2/"+param;
var params = JSON.stringify({ param: someValue });
http.open("POST", url, true);
http.setRequestHeader("Content-type", "application/json; charset=utf-8");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");
http.onreadystatechange = function() {
if(http.readyState == 4 && http.status == 200) {
alert(http.responseText);
//or do some callback
}
}
http.send(params);