Announcement Announcement Module
No announcement yet.
Posting json? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Posting json?

    I currently have this working with a roo generated project to pull json formatted objects from the server, works good using the jackson library:

           ObjectMapper mapper = new ObjectMapper();
            mapper.configure(Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        	MyWrappedClass wrapper = null;
    	wrapper = mapper.readValue(new URL(""), MyWrappedClass.class);
    Now I would like to push an edited object back up to the server and process it, does anyone have some suggestions for the best way to get this done? At first glance I was thinking I would have to create some custom server side code and pickup an HTTP post that contains the json and then invoke the objects to do the save back to the DB (assuming what I wanted to do was save, or whatever the server side operation is).

    A fantasy implementation at the client would look like:

           ObjectMapper mapper = new ObjectMapper();
           string  jsonValue = mapper.writeValueAsString(wrapper);
          rooServer.WriteJson(URL(""), jsonValue);

  • #2
    Here is something that doesn't work, as far as I can tell every single PUT to http://localhost:8080/MyService/MyDataItem/json results in an empty object being created on the server side (the MyData.Data property is empty).

    		DefaultHttpClient httpclient = new DefaultHttpClient();
    		HttpPut httpput = new HttpPut("http://localhost:8080/MyService/MyDataItem");
    		DataItemJsonWrapper item = new DataItemSingleJsonWrapper();
    		String jsonValue = mapper.writeValueAsString(item);
    		StringEntity se = new StringEntity(jsonValue);
    		httpput.setHeader("Accept", "application/json");
    		httpput.setHeader("Content-type", "application/json");
    		ResponseHandler responseHandler = new BasicResponseHandler();
    		Object response = httpclient.execute(httpput, responseHandler);


    • #3
      Have you taken a look at Keith's example on the SpringSource blog He is using JSON for remoting between his JQuery views and Spring MVC. I believe this is exactly what you are looking for.



      • #4
        thank you, with some finagaling here is what I came up with:

        1. Define another controller that handles the url you are interested appended with jsonin. I did this because if I put the RequestMethod.Post on the main auto generated controller it would screw up the web view stuff (form posts wouldn't work). By the way if you do this on the auto gened controller, it can be sticky to get things back to working, I ended up having to create a dummy field and then removed the dummy field to get something somewhere to reset (guessing something in xml file got hosed or something).

        2. The JSON needs to be formatted as one simplistic row of data.

        Given an object named MyData, the JSON generated by jackson will look something like this:

        {"MyData" : { "id":2, "name":"John" }}

        if you post that via a call like this:

        	StringEntity se = new StringEntity(jsonValue);
        	httppost.setHeader("Accept", "application/json");
        	httppost.setHeader("Content-type", "application/json");
        	ResponseHandler responseHandler = new BasicResponseHandler();
        	Object response = httpclient.execute(httppost, responseHandler);
        you get an error where Jackson on the server side whines about MyData.

        So if you change it to look like: { "id":2, "name":"John" }

        Then it works much better and you can call MyData.persist or merge or whatever is appropriate on the server side.