Java 可以为请求传递json对象以获取我正在尝试的内容,但我错过了以下错误
可以为请求传递json对象以获取我正在尝试的内容,但我错过了以下错误Java 可以为请求传递json对象以获取我正在尝试的内容,但我错过了以下错误,java,json,rest,post,get,Java,Json,Rest,Post,Get,可以为请求传递json对象以获取我正在尝试的内容,但我错过了以下错误 Caused by: java.net.URISyntaxException: Illegal character in query at index 46: https://localhost/Pro-Ing/rest/pedidos/prueba?{%22codigo%22:%22qwdasdas%22,%22id%22:%221%22} 这是javascript方法 function hola(){ var id= p
Caused by: java.net.URISyntaxException: Illegal character in query at index 46: https://localhost/Pro-Ing/rest/pedidos/prueba?{%22codigo%22:%22qwdasdas%22,%22id%22:%221%22}
这是javascript方法
function hola(){
var id= prompt('hola');
var id2= prompt('hola');
codigo = {};
codigo['codigo'] = id;
codigo['id'] = id2;
$.ajax({
url : "rest/pedidos/prueba",
contentType : "application/json",
dataType : "json",
type : "GET",
data : JSON.stringify(codigo),
success : function(data) {
jqmSimpleMessage('Paso');
}
error : function(error) {
if (error.status == 401) {
desAuth();
} else {
jqmSimpleMessage("error -" + error.responseText);
}
},
beforeSend : function(xhr, settings) {
xhr.setRequestHeader('Authorization', 'Bearer '
+ getVCookie(getVCookie("userPro")));
}
});
}
这是接收对象的java方法
@GET
@Path("/prueba")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response gendup(confirmacion usu,@Context SecurityContext securityContext) {
registro(securityContext, 0, "");
confirmacion confirmacion = null;
Response.ResponseBuilder builder = null;
return builder.build();
}
Java中的数组是基于零的,所以
01234567890123456789012345678901234567890123456
https://localhost/Pro-Ing/rest/pedidos/prueba?{%22codigo%22:%22qwdasdas%22,%22id%22:%221%22
^
描述查询中允许使用哪些字符
query = *( pchar / "/" / "?" )
pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
pct-encoded = "%" HEXDIG HEXDIG
sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
/ "*" / "+" / "," / ";" / "="
因此,这里的问题是,java解析器对URI中允许哪些字符是严格的,左花括号{
和右花括号}
都是无效的——它们也需要进行百分比编码
https://localhost/Pro-Ing/rest/pedidos/prueba?%7B%22codigo%22:%22qwdasdas%22,%22id%22:%221%22%7D
此URI(按规范要求对括号百分比进行编码)应满足java代码的要求
我猜JSON.stringify正在做我们期望的事情:
> JSON.stringify({"codingo":"qwdasdas","id":"1"})
'{"codingo":"qwdasdas","id":"1"}'
根据您提供的信息,对我来说没有任何意义的是,为什么您得到的查询中引号是百分比编码的,而不是左括号和右括号。您的URI看起来像是有人决定使用自己的URI编码器,但没有正确处理所有“特殊字符”
验证这是否是java脚本方面的问题(而不是服务器上运行的java)的一种方法是查看正在生成的HTTP请求,并验证用作请求目标uri的值:如果请求中的查询拼写无效,那么服务器肯定与问题无关(除报告外).我想我们需要更多的信息。由于没有完整的堆栈跟踪,因此很难看到引发异常的位置。引发的异常不是由任何javascript引起的,因为它是java异常。如果您将url更改为以下内容,它可能会起作用:
https://localhost/Pro-Ing/rest/pedidos/prueba?%7b%22codigo%22:%22qwdasdas%22,%22id%22:%221%22%7d
> JSON.stringify({"codingo":"qwdasdas","id":"1"})
'{"codingo":"qwdasdas","id":"1"}'