Spring Boot(二)--- 参数

来源:互联网 发布:淘宝众筹是骗局吗 编辑:程序博客网 时间:2024/06/06 13:24

@RestController 相当于返回字符串数据 (json) 的控制器

@RequestMapping 配置访问地址

@RequestMapping("/") 表示配置根地址 -->http://127.0.0.1:8080/@RequestMapping("/login") -->http://127.0.0.1:8080/login

可变的子路径

@RequestMapping("/seach/{kw}")public String searchUser(@PathVariable("kw") String name){    return "---->"+name;}

注意 @PathVariable 后面的名称要跟子路径大括号中的名称一样 , 如果是基本数据类型,建议使用封装类
(Integer,Character,Double,Float,Boolean,Byte,Short,Long)

设置请求方式,默认情况下每个地址都支持 get 以及 post ,如果只要求支持一个,需要单独设置

@RequestMapping(value="/testGet",method=RequestMethod.GET)public String testGet(){    return "------get----";}

关于请求参数的配置,直接在方法上加参数即可

@RequestMapping(value="/testGet",method=RequestMethod.GET)public String testGet(String name,String psw){    return "------get----"+name+" | "+psw;}

请求地址: http://127.0.0.1:8080/testGet?name=abc&psw=123

注意:参数如果是基本数据类型要用封装类 (Integer,Character,Double,Float,Boolean,Byte,Short,Long)

Android Http 网络请求

Android 网络请求要放在子线程中

Get 请求

private void httpGet(){new Thread(){    @Override    public void run() {        URL url;        try {            // 创建链接对象            url = new URL("http://192.168.18.251:8080/testGet?name=android_get&psw=123456");            // 打开链接            HttpURLConnection conn = (HttpURLConnection) url.openConnection();            // 检测请求结果            if(conn.getResponseCode() == 200){                // 获取内容                InputStream in = conn.getInputStream();                // 借助 ByteArrayOutputStream 将 InputStream 转为字符串                ByteArrayOutputStream out = new ByteArrayOutputStream();                byte[] buf = new byte[1024];                int num;                while((num = in.read(buf))!=-1){                    out.write(buf,0,num);                }                out.flush();                String resutString = new String(out.toByteArray());                // 显示结果                mHandler.obtainMessage(1,resutString).sendToTarget();                out.close();                in.close();                }else{                    mHandler.sendEmptyMessage(0);                }                conn.disconnect();            } catch (Exception e) {                    e.printStackTrace();                    mHandler.sendEmptyMessage(0);            }        }    }.start();}private Handler mHandler = new Handler(){    @Override    public void handleMessage(Message msg) {        if(msg.what == 1){        String str = (String) msg.obj;        tvResult.setText(str);        }else{            tvResult.setText(" 请求异常 ");        }    }};

Post 请求

private void httpPost() {    new Thread(){        @Override        public void run() {            URL url;            try {                url = new URL("http://192.168.18.251:8080/testPost");                HttpURLConnection conn = (HttpURLConnection) url.openConnection();                conn.setDoInput(true);                conn.setDoOutput(true);                // 设置请求方式                conn.setRequestMethod("POST");                conn.connect();                // 写参数到服务器端                String par = "name=android_post&psw=abc123";                OutputStreamWriter outW = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");                outW.write(par);                outW.flush();                outW.close();                if (conn.getResponseCode() == 200) {                    InputStream in = conn.getInputStream();                    // 借助 ByteArrayOutputStream 将 InputStream 转为字符串                    ByteArrayOutputStream out = new ByteArrayOutputStream();                    byte[] buf = new byte[1024];                    int num;                    while ((num = in.read(buf)) != -1) {                        out.write(buf, 0, num);                    }                    out.flush();                    String resutString = new String(out.toByteArray());                    // 显示结果                    mHandler.obtainMessage(1, resutString).sendToTarget();                    out.close();                    in.close();                }else{                    mHandler.sendEmptyMessage(0);                }                conn.disconnect();            } catch (Exception e) {                e.printStackTrace();                mHandler.sendEmptyMessage(0);            }        }    }.start();}
原创粉丝点击