Panagiotis Drakatos
Panagiotis Drakatos

Reputation: 3214

Pushing real-time data from Spring MVC Controller

i've made a simple Rest android Client which send a request to A Spring mvc controller.

This is the android code

package security.com.testing;

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText editTextName;
    private EditText editTextUsername;
    private EditText editTextPassword;
    private EditText editTextEmail;

    private Button buttonRegister;
    //This is our root url
    public static final String ROOT_URL = "http://192.168.1.68/";
    private Map<String, String> vars;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        vars = new HashMap<String, String>();
        vars.put("id", "JS01");
        new HttpRequestTask().execute();

    }


    //Overriding onclick method
    @Override
    public void onClick(View v) {
        //Calling insertUser on button click

    }


    private class HttpRequestTask extends AsyncTask<Void, Void, User> {
        @Override
        protected User doInBackground(Void... params) {
            try {
                final String url = "http://192.168.88.1:8080/api/{id}";

                User u = new User();
                u.setName("Johnathan M Smith");
                u.setUser("JS01");


                RestTemplate restTemplate = new RestTemplate();
                restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
                User us = restTemplate.postForObject(url, u, User.class, vars);
                return us;
            } catch (Exception e) {
                Log.e("MainActivity", e.getMessage(), e);
            }

            return null;
        }

        protected void onPostExecute(Greeting greeting) {
        }

    }

}

My MVC Controller Accepts the android Request(User object ) with success.

Here is my Mvc Controller Code

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/api")
public class GreetingController {

    @RequestMapping(value = "/{id}", method = RequestMethod.POST)
    @ResponseBody
    public String updateCustomer(@PathVariable("id") String id, @RequestBody User user,Model model) {
        System.out.println("I am in the controller and got user name: " +user.getName());
        model.addAttribute("name", user.getName());
        return "result";
    }
}

My problem now is that i want to update the browser when Mvc controller accepts the value of the request in real time. The request going through every 3 min and i want the browser be notified(when Mvc controller accepts requests) . How should i do this?.i am not sure but i think i should use reverse ajax or cometd. Can you help me achieve it,sorry for my stupid question but i am so confused and i can't find the proper way to solve my problem.

Upvotes: 0

Views: 3426

Answers (1)

Panagiotis Drakatos
Panagiotis Drakatos

Reputation: 3214

Option A: I found solution to my problem. I used websockets for Real-time data updates with the browser.An HTML file imports the SockJS and STOMP javascript libraries that will be used to communicate with my server using STOMP over websocket. Take a closer look Here. Springframework has own api that implements messaging using the new WebSocket capabilities witch introduced with Spring Framework 4.0

Option B:Another solution is to use Atmoshpere which is a Realtime Client Server Framework for the JVM, supporting WebSockets with Cross-Browser Fallbacks

Upvotes: 1

Related Questions