美文网首页
springMVC之@PathVariable应用实例

springMVC之@PathVariable应用实例

作者: haiyong6 | 来源:发表于2020-04-19 14:14 被阅读0次

从spring3.0开始,springMVC加入了@PathVariable 用来映射 URL 绑定的占位符。
也就是说在请求的url比如/example/test
后台接口:

@RequestMapping("/example/{environment}")
@ResponseBody
public String test(HttpServletResponse response, HttpServletRequest request,
            @PathVariable("environment") String environment){
     System.out.println(environment);
return null;
}

此时后台能够打印出test字符串。

有了@PathVariable,就可以很方便的拿到请求的url里的某些字段,而不用写多个接口。
比如最近有个需求,客户远程访问应用拿数据,对应不同的url拿不同的数据库表里的数据,url就是表名。
如果按照普通的做法,拿20个表对应20个不同的url,那就要写20个接口,但是处理逻辑其实都是一样的,这种情况@PathVariable就格外方便了,只需要写一个接口:

    @GetMapping("/{id}")
    @ResponseBody
    public String getTableData(HttpServletResponse response, HttpServletRequest request,
            @PathVariable("id") String id) {
        List<String> list = new ArrayList<String>();
        try {
            list = IOUtil.readPropertiesValueList("sendTable.properties");
        } catch (IOException e) {
            e.printStackTrace();
        }

        String dataType = "error";
        for (int i = 0; i < list.size(); i++) {
            if (id.equals(list.get(i))) {
                dataType = id;
                break;
            }
        }
        if ("error".equals(dataType)) {
            return "无效的链接!";
        }
        // final String DATATYPE = "DM_DEALER_NET_PRICE_DATE";
        return download(response, request, dataType);
    }

其中的id为不同的表名,这样就可以匹配不同的url了,把不同的表名写到properties文件里统一管理,java读取properties,一个接口就可以搞定,根据获取的id不同,下载不同的信息。
IOUtil.java

package com.test.app.utils;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class IOUtil {
    
    public static Map<String,String> readProperties(String path) throws IOException{
        Map<String,String> result = new HashMap<String,String>();
        InputStream in = null;
        try{
            in = new BufferedInputStream (new FileInputStream(path));
            Properties prop = new Properties();
            prop.load(in);
            
            Iterator<String> it = prop.stringPropertyNames().iterator();
            while(it.hasNext()){
                String key = it.next();
                String value = prop.getProperty(key);
                result.put(key, value);
            }
        }catch(Exception e){
            System.out.println(e.getMessage());
        }finally{
            in.close();
        }
        return result;
    }
    
    public static List<String> readPropertiesValueList(String path) throws IOException{
        List<String> list = new ArrayList<String>();
        InputStream in = null;
        try{
            in = new BufferedInputStream (IOUtil.class.getClassLoader().getResourceAsStream(path));
            Properties prop = new Properties();
            prop.load(in);
            
            Iterator<String> it = prop.stringPropertyNames().iterator();
            while(it.hasNext()){
                String key = it.next();
                String value = prop.getProperty(key);
                list.add(value);
            }
        }catch(Exception e){
            System.out.println(e.getMessage());
        }finally{
            in.close();
        }
        return list;
    }
    
    public static String readFile(String path){
        StringBuffer sb = new StringBuffer();
        File file = new File(path);  
        BufferedReader reader = null;  
        try {  
            reader = new BufferedReader(new FileReader(file));  
            String line = "";  
            while ((line = reader.readLine()) != null) {  
                sb.append(line);
            }  
        } catch (IOException e) {  
            e.printStackTrace();  
        } finally {  
            if (reader != null) {  
                try {  
                    reader.close();  
                } catch (IOException e1) { 
                    e1.printStackTrace();
                }  
            }  
        }  
        return sb.toString();
    }
    
    
    public static void writeFile(String path ,String text)throws IOException{
        OutputStreamWriter osw = null;
        try {
           osw = new OutputStreamWriter(new FileOutputStream(path,true), "UTF-8");   
           osw.write(text);   
           osw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            osw.close();
        }
    }
    
}

相关文章

网友评论

      本文标题:springMVC之@PathVariable应用实例

      本文链接:https://www.haomeiwen.com/subject/rtupbhtx.html