将json发布到Java服务器

将json发布到Java服务器

本文介绍了将json发布到Java服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在Internet上搜索一些,以寻找(正确)@消耗服务器上网络资源中的"application/json"文件的方法.

I've been googleing a bit around internet looking for a (correctly) way to @Consume an "application/json" file in a web resource on my server.

我正在使用glassfish应用服务器,因此它是Java资源.

I'm using glassfish app server, so it's a java resource.

这是调用javascvript的代码:

here is the calling javascvript code:

        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字符串.

where json2Send is the json String the client has to send to the server.

这里是服务器端代码:

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

我现在正在问你,为了让服务器读取来自javascript的输入json字符串,我必须做什么.显然,我上面描述的方法行不通.服务器返回

I'm now asking to you what do I have to do in order to let the server read the input json string coming from the javascript.Obviously, the way I described above doesn't work. the server returns

HTTP Status 405 -

type Status report

message

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

通过互联网查看我的问题,我发现了涉及"BufferedReader"类的"readline()"方法的解决方案.我不喜欢这种解决方案.我更喜欢(如果有这样的话)注入json文件,而不是逐行读取输入字符串.

looking my problem over the internet, I've found solutions involving "readline()" method of the "BufferedReader" class. I do not like this solution. I prefer, if ther is a way, to inject the json file instead to read line by line the input string.

任何帮助都是可以接受的谢谢

any help is well acceptedthanks

推荐答案

很高兴能为您提供帮助.

Glad I could help.

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

I still recommend constructing your json2Send using actual Javascript Object Notation (JSON) instead of string concatenation, e.g. like this:

// 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的字符串,如下所示:

and then converting it to a string for the HTTP POST like this:

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

这将更早地发现语法错误,使您不必手动引用所有不同的片段,这很有可能会更快,并且可以确保整个过程更加健壮.

This will catch syntax errors much earlier, relieves you from manually quoting all the different bits and pieces, is most likely faster and it sure makes the whole thing a lot more robust.

这篇关于将json发布到Java服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 06:45