简体   繁体   English

从Spring MVC Controller推送实时数据

[英]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. 我做了一个简单的Rest android客户端,它向Spring Mvc控制器发送请求。

This is the android code 这是android代码

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. 我的MVC控制器成功接受android Request(User object)。

Here is my Mvc Controller Code 这是我的Mvc控制器代码

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. 我现在的问题是,当Mvc控制器实时接受请求的值时,我想更新浏览器。 The request going through every 3 min and i want the browser be notified(when Mvc controller accepts requests) . 该请求每3分钟执行一次,我希望浏览器得到通知(当Mvc控制器接受请求时)。 How should i do this?.i am not sure but i think i should use reverse ajax or cometd. 我应该怎么做?。我不确定,但我认为我应该使用反向Ajax或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. 对不起我的愚蠢问题,对不起,您能帮助我实现它吗,但我很困惑,我找不到解决问题的正确方法。

Option A: I found solution to my problem. 选项A:我找到了解决问题的方法。 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. 我使用websocket通过浏览器进行实时数据更新。一个HTML文件导入了SockJS和STOMP javascript库,这些库将用于通过websocket使用STOMP与服务器进行通信。 Take a closer look Here . 在这里仔细看看。 Springframework has own api that implements messaging using the new WebSocket capabilities witch introduced with Spring Framework 4.0 Springframework拥有自己的api,可使用Spring Framework 4.0引入的新WebSocket功能实现消息传递

Option B: Another solution is to use Atmoshpere which is a Realtime Client Server Framework for the JVM, supporting WebSockets with Cross-Browser Fallbacks 选项B:另一个解决方案是使用Atmoshpere ,它是用于JVM的实时客户端服务器框架,支持具有跨浏览器后备功能的WebSocket

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM