【问题标题】:post json to java server将 json 发布到 java 服务器
【发布时间】:2012-06-25 08:18:49
【问题描述】:

我一直在互联网上搜索一些(正确的)方法来@Consume 在我的服务器上的网络资源中的“应用程序/json”文件。

我使用的是 glassfish 应用服务器,所以它是一个 java 资源。

这里是调用 javascvript 的代码:

        var url="/MBC/pages/lives/";
        var mygetrequest=new ajaxRequest();
        mygetrequest.onreadystatechange=function(){
         if (mygetrequest.readyState==4){
          if (mygetrequest.status==200 || window.location.href.indexOf("http")==-1){
              var render="";

              var JsonIn =JSON.parse(mygetrequest.responseText);

              if(JsonIn.error==undefined){
                    render="generic error"; 
                  }
               }else
                  render=mygetrequest.responseText  ;

               document.getElementById(div).innerHTML=render;

            }else{
           render="An error has occured making the request";
          } 
        };
        var json2Send = "{" +
                "boss:\""+location.href.substring(location.href.length-5,location.href.length-4)+"\"," ;
        if(document.newLive.bval.value=='')
            json2Send+="bands:[],";
        else
            json2Send+="bands:["+document.newLive.bval.value+"],";

        json2Send+="data:\""+document.newLive.dateEvent.value+"\"," +
                "address:{street:\""+document.newLive.street.value+"\"," +
                        "number:\""+document.newLive.number.value+"\"," +
                        "city:\""+document.newLive.city.value+"\"," +
                        "region:\""+document.newLive.region.value+"\"," +
                        "state:\""+document.newLive.state.value+"\"}" +
                "}";
        mygetrequest.open("POST", url, true);
        mygetrequest.setRequestHeader("Content-type", "application/json");
        mygetrequest.send(json2Send);

其中 json2Send 是客户端必须发送到服务器的 json 字符串。

这里是服务器端代码:

@POST
@Path("configLiveBand")
@Consumes("application/json")
@Produces("application/json")
public String liveBandInsert(String jsonIn, @Context HttpServletRequest request) throws ParseException{

我现在问你我必须做什么才能让服务器读取来自 javascript 的输入 json 字符串。 显然,我上面描述的方式是行不通的。服务器返回

HTTP Status 405 -

type Status report

message

descriptionThe specified HTTP method is not allowed for the requested resource ().

通过互联网查看我的问题,我找到了涉及“BufferedReader”类的“readline()”方法的解决方案。我不喜欢这个解决方案。如果有办法,我更喜欢注入 json 文件而不是逐行读取输入字符串。

任何帮助都会被接受 谢谢

【问题讨论】:

  • 当然,请告诉我是否需要说明与我尚未写的问题相关的其他内容。
  • 如果删除@Produces@Consumes 注释,是否会调用liveBandInsert()?您的浏览器发送的请求实际上是什么样的(检查浏览器的开发人员工具)?如果您使用curl 之类的内容发布到具有相同示例数据的 URL,它是否有效?如果您删除@Consumes@Produces 并更改为@GET,您能否在浏览器中访问该URL?请在问题中回答,cmets可能太小了:)
  • 如果我删除 @Produces@Consumes 注释,服务器将返回相同的 405 status 并返回与以前相同的描述。你知道吗?问题是@Post 注释。它适用于 get 但不适用于 @POST。所以,这是一个javascript问题。我没有正确地向服务器发送发布请求。让我发布整个通话...
  • 为什么要使用字符串而不是实际的 Javascript 对象来构建 JSON? json2Send 的内容实际上是有效的 JSON(使用在线验证器如 jsonlint.com 检查)吗?
  • 亲爱的 Philipp,你说的完全正确。

标签: java json jax-rs inject


【解决方案1】:

很高兴我能帮上忙。

我仍然建议使用实际的 Javascript 对象表示法 (JSON) 而不是字符串连接来构建您的 json2Send,例如像这样:

// This creates an "empty" JS object, with no properties.
var json2Send = new Object();
var length = location.href.length;
// Adding a property is as easy as just setting it, it will be created by this.
json2Send.boss = location.href.substring(length - 5, length - 4);
if (document.newLive.bval.value == '') {
  json2Send.bands = [];
} else {
  json2Send.bands = [document.newLive.bval.value];
}
json2Send.data = document.newLive.dateEvent.value;
// Properties can also be other objects, here created with the
// object literal notation of { key:value, ...}.
json2Send.address = {
  // Inside, it's just another JS object again,
  // this time setting properties with literal notation key:value
  // Note how there's no need to quote anything here!
  street: document.newLive.street.value,
  number: document.newLive.number.value,
  city: document.newLive.city.value,
  region: document.newLive.region.value,
  state: document.newLive.state.value
};

然后将其转换为 HTTP POST 的字符串,如下所示:

mygetrequest.open("POST", url, true);
mygetrequest.setRequestHeader("Content-type", "application/json");
mygetrequest.send(JSON.stringify(json2Send));

这将更早地捕获语法错误,使您无需手动引用所有不同的点点滴滴,而且很可能更快,并且确实使整个事情更加健壮。

【讨论】:

    【解决方案2】:

    问题在于 Philipp 所写的 json:

    json2Send+="\"data\":\""+document.newLive.dateEvent.value+"\"," +
            "\"address\":{\"street\":\""+document.newLive.street.value+"\"," +
                    "\"number\":\""+document.newLive.number.value+"\"," +
                    "\"city\":\""+document.newLive.city.value+"\"," +
                    "\"region\":\""+document.newLive.region.value+"\"," +
                    "\"state\":\""+document.newLive.state.value+"\"}" +
            "}";
    

    而不是

    json2Send+="data:\""+document.newLive.dateEvent.value+"\"," +
            "address:{street:\""+document.newLive.street.value+"\"," +
                    "number:\""+document.newLive.number.value+"\"," +
                    "city:\""+document.newLive.city.value+"\"," +
                    "region:\""+document.newLive.region.value+"\"," +
                    "state:\""+document.newLive.state.value+"\"}" +
            "}";
    

    现在我可以读取、发布 json 结果并将其发送回客户端。 ;-) 谢谢菲利普

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多