gpt4 book ai didi

java - 调用maven项目的困惑

转载 作者:行者123 更新时间:2023-12-02 03:35:46 25 4
gpt4 key购买 nike

我有一个 Maven 项目,它在我的系统上成功运行。该 Maven 项目将任何 Web URL 作为输入。现在我想每当我的 chrome 扩展被点击时调用这个 Maven 项目。我的 Chrome 扩展能够获取网站的 URL。

那么我该怎么做呢?在搜索谷歌和许多其他资源时,我开始知道我必须制作一个可以监听客户端行为的安静的网络服务。

但是我仍然不清楚如何做到这一点?我连最初的步骤都不清楚?

任何想法都会对我有很大帮助!

我对 REST 端点/ postman 一无所知,这只是我收集的信息。

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();
}

}

我尝试将华氏温度值发送到 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

最佳答案

如果您编写了 REST Controller ,它应该启动并监听传入的请求。您需要提供这些端点作为请求 url 并在您的扩展中获取信息。您可以直接使用浏览器或使用 POSTMAN 等扩展来检查和测试您的 REST 端点。你可以做一个 XMLHttpRequest从 chrome 扩展程序到您的 REST 端点。

用于调用 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);

关于java - 调用maven项目的困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37454029/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com