java - 可以传递一个 json 对象来获取我正在尝试的请求,但我错过了以下错误

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

最佳答案

Illegal character in query at index 46: https://localhost/Pro-Ing/rest/pedidos/prueba?{%22codigo%22:%22qwdasdas%22,%22id%22:%221%22}

Java 中的数组是从零开始的,所以

01234567890123456789012345678901234567890123456
https://localhost/Pro-Ing/rest/pedidos/prueba?{%22codigo%22:%22qwdasdas%22,%22id%22:%221%22
                                              ^

RFC 3986, Appendix A 描述查询中允许使用哪些字符

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 请求,并验证用作 target-uri 的值请求:如果查询请求中存在无效拼写,那么服务器肯定与该问题无关(除了报告该问题之外)。

关于java - 可以传递一个 json 对象来获取我正在尝试的请求,但我错过了以下错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56138583/

相关文章:

java - 通过修改desktop.ini来更改图标的文件夹

java - Android ArrayList 更新值

json - 收到不支持的媒体类型错误

javascript - Laravel - Javascript 在 JSFiddle 中工作,但在 Blade 中不起作用

rest - 将 REST 请求与异步服务器中的响应相匹配

java - 在执行 .toString() 后如何取回对象?

java - 将线程从 sleep 状态唤醒

c# - 如何在 C# 中解析 json 列表

python - DRF - 如何使用 Oauth Toolkit 验证应用程序?

json - 混合GSP和JSON View 时如何设置首选 View 分辨率