我有一个REStful webservice(java,jersey)来做一些事情。 用REST控制器调用ajax请求(GET或POST)的每个函数都可以正常工作...除了最近的那些,我不知道为什么......我尝试了所有的东西并坚持了这个问题将近三天(写了3个不同的函数,从GET更改为POST,用新的pathannotiation重写了函数,试图调用pageload ..重命名一切),我真的很感激任何可以帮助我...
如果url包含rest / *,则控制器将其转发到实现所需函数的类。
JS功能
function testFunc() {
$.ajax({
url: "rest/name/wut",
type: "GET",
contentType: "application/json;charset=utf-8",
success: function(response) {
alert("LSKDFJLSDKJFLKSD " + response);
},
error: function(response) {
alert("ma oidaaaa " + JSON.stringify(response));
}
});
};
RESTClass中的Java代码......
@GET
@Path("/wut")
@Produces(MediaType.APPLICATION_JSON)
private String wut() {
JSONObject json = new JSONObject();
json.put("print", "wuut");
return json.toString();
}
如果该方法正在做任何有用的事情并不重要......它只返回404没有找到任何元素。 (它甚至没有被调用)因此我在RESTClass中尝试了不同的新方法......比如:
@GET
@Path("/wut")
@Produces(MediaType.APPLICATION_JSON)
private String wut() throws IOException {
URL url = new URL(url);
StringBuffer response = new StringBuffer();
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
BufferedReader in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
return response.toString();
}
我正在使用另一种方法剪切,这是有效的..我用" url"替换了真实的URL。发布btw。
我也尝试了不同的MediaTypes:WILDCARD,TEXT / PLAIN ...... 并且只返回一个String ...
任何人都有任何想法(对于糟糕的英语感到抱歉,我真的绝望,所以我没有做拼写检查,英语不是我的母语:()
答案 0 :(得分:1)
两个想法:
public String wut()
http://localhost/rest/name/wut
,看看会发生什么答案 1 :(得分:0)
我会尝试使用绝对路径: 变化:
url: "rest/name/wut",
到
url: "/rest/name/wut",
错误消息告诉我,您的客户端没有尝试服务器提供的地址。