Announcement Announcement Module
Collapse
No announcement yet.
Posting json? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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:

    Code:
           ObjectMapper mapper = new ObjectMapper();
            
            mapper.configure(Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    
        	MyWrappedClass wrapper = null;
    	wrapper = mapper.readValue(new URL("http://10.0.2.2:8080/MyProject/MyClass.json"), 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:

    Code:
           ObjectMapper mapper = new ObjectMapper();
           string  jsonValue = mapper.writeValueAsString(wrapper);
          rooServer.WriteJson(URL("http://10.0.2.2:8080/MyProject/MyClass.json"), 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).

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

    Comment


    • #3
      Have you taken a look at Keith's example on the SpringSource blog http://blog.springsource.com/2010/01...n-spring-3-0/? He is using JSON for remoting between his JQuery views and Spring MVC. I believe this is exactly what you are looking for.

      -Stefan

      Comment


      • #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:

        Code:
        	StringEntity se = new StringEntity(jsonValue);
        	httppost.setEntity(se);
        	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.

        Comment

        Working...
        X