0

I am working on a JavaSE application in which I would like to connect to a Spring-MVC based server to get List of objects, Objects itself. I looked up on net, and came upon JSON. While I agree that it is working, but it is very inefficient as I have to go through the 2 while loops and seems not so sophisticated. For this reason I researched and found out I can use Spring remoting to achieve the task.

One thing I would like to do is to send over objects directly, instead of converting them by JSON, and sending.

I am pasting my code below for what I have with JSON, I would appreciate if I know this seems more better or is Spring remoting more sophisticated in long term too. A replacement code for the client side would be nice. Thanks.

Client code :

 public void getCanvas(){
     JsonFactory jsonFactory = new JsonFactory();
        String canvas = "";
        try {
            JsonParser jsonParser = jsonFactory.createJsonParser(new URL(canvasURL));
            JsonToken token = jsonParser.nextToken();

           while (token!=JsonToken.START_ARRAY && token!=null){
               token = jsonParser.nextToken();
               if(token==null){break;}
               System.out.println("Token is "+jsonParser.getText());
           }
            while (token!=JsonToken.END_ARRAY){
                token = jsonParser.nextToken();
                if(token == JsonToken.START_OBJECT){
                    canvas = jsonParser.toString();
                    System.out.println("Canvas is "+canvas);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("Canvas is "+canvas);
    }

Server code :

 @RequestMapping(value = "/getcanvas",method = RequestMethod.GET)
    public @ResponseBody String getCanvasforFX(){
        System.out.println("Canvas was requested");
        Canvas canvas = this.canvasService.getCanvasById(10650);
        canvas.setCanvasimage(null);
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            System.out.println("Canvas value is "+objectMapper.writeValueAsString(canvas));
            return objectMapper.writeValueAsString(canvas);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

In the client-code I am getting the information, but again I have to read the fields, set them in object and update the UI, even though I am programming the server also, I want to directly receive an object, and cut out the middle-man(JSON). Thanks.

We are Borg
  • 5,117
  • 17
  • 102
  • 225
  • 1
    Spring Remoting is RPC and for loosely coupled this isn't a very good thing. You shouldn't be messing around with that JSON stuff yourself Spring has very good support for it (using Jackson). On the client use a `RestTemplate` and use `@RequestBody` and `@ResponseBody` on the server to automatically convert objects, don't do it yourself as you are doing now. – M. Deinum May 18 '15 at 09:38
  • @M.Deinum : Thank you, I will have a look into it. – We are Borg May 18 '15 at 09:55
  • @M.Deinum : Thank you, It worked like a charm with RestTemplate. I just have one question, in the spring-mvc project(Server), there are database mappings in Model classes. Now, when I get a class Object by id, because of lazy loading, there are null values in the mapping, and no such mappings exist in the client side Java program I am making, because of that I get premature EOF, how would you suggest I can remedy this other then creating new REST based Model classes. Thanks. – We are Borg May 19 '15 at 09:45

0 Answers0