Selaa lähdekoodia

1. 上下文Context操作范例
2. 数据请求操作范例
3. 会话Session的操作范例,支持redis

huangbo 6 vuotta sitten
vanhempi
commit
369e046cb8

+ 31 - 15
ipu-rest-demo/src/main/java/com/ai/ipu/server/demo/control/data/ContextDataController.java

@ -6,25 +6,29 @@ import org.springframework.stereotype.Controller;
6 6
import org.springframework.web.bind.annotation.RequestMapping;
7 7
import org.springframework.web.bind.annotation.ResponseBody;
8 8

9
import com.ai.ipu.data.JMap;
10
import com.ai.ipu.data.impl.JsonMap;
9 11
import com.ai.ipu.restful.frame.context.IpuContextData;
10
import com.ai.ipu.restful.util.IpuRestConstant;
11 12
import com.ai.ipu.restful.web.ServletManager;
12
import com.ailk.common.data.IData;
13
import com.ailk.common.data.impl.DataMap;
14 13

15 14
/**
16 15
 * @author huangbo@asiainfo.com
17 16
 * @team IPU
18 17
 * @date 2018年11月7日下午11:36:42
19
 * @desc 上下文使用范例
18
 * @desc 上下文Context操作范例
20 19
 */
21 20
@Controller
22 21
@RequestMapping("/context")
23 22
public class ContextDataController {
24 23
    
24
    /**
25
     * @author huangbo@asiainfo.com
26
     * @title: createContext
27
     * @desc: 创建上下文Context的范例
28
     */
25 29
    @ResponseBody
26 30
    @RequestMapping("/create")
27
    public IData createContext(IData param) {
31
    public JMap createContext(JMap param) {
28 32
        // TODO Auto-generated method stub
29 33
        IpuContextData ipuContextData = new IpuContextData();
30 34
        Iterator<String> it = param.keySet().iterator();
@ -33,34 +37,46 @@ public class ContextDataController {
33 37
            key = it.next();
34 38
            ipuContextData.put(key, param.get(key).toString());
35 39
        }
36
        ServletManager.getSession().setAttribute(IpuRestConstant.IPU_CONTEXT, ipuContextData.toString());
37
        IData result = new DataMap();
38
        result.put("msg", "上下文创建成功");
40
        ServletManager.storageContextData(ipuContextData);
41
        JMap result = new JsonMap();
42
        result.put("msg", "创建上下文");
39 43
        return result;
40 44
    }
41 45
    
46
    /**
47
     * @author huangbo@asiainfo.com
48
     * @title: updateContext
49
     * @desc: 修改上下文Context的范例
50
     */
42 51
    @ResponseBody
43 52
    @RequestMapping("/update")
44
    public IData updateContext(IData param) {
53
    public JMap updateContext(JMap param) {
45 54
        IpuContextData ipuContextData = ServletManager.getContextData();
46 55
        Iterator<String> it = param.keySet().iterator();
47 56
        String key;
57
        /*请求结束时,上下文如果有修改,则回写到Session中*/
48 58
        while(it.hasNext()){
49 59
            key = it.next();
50 60
            ipuContextData.put(key, param.get(key).toString());
51 61
        }
52 62
        
53
        IData result = new DataMap();
54
        result.put("msg", "上下文修改成功");
63
        JMap result = new JsonMap();
64
        result.put("msg", "修改上下文");
55 65
        return result;
56 66
    }
57 67
    
68
    /**
69
     * @author huangbo@asiainfo.com
70
     * @title: takeContext
71
     * @desc: 获取上下文Context的范例
72
     */
58 73
    @ResponseBody
59
    @RequestMapping("/get")
60
    public IData getContext(IData params) {
74
    @RequestMapping("/take")
75
    public JMap takeContext(JMap params) {
61 76
        IpuContextData ipuContextData = ServletManager.getContextData();
62
        IData result = new DataMap();
63
        result.put("context", ipuContextData.toString());
77
        JMap result = new JsonMap();
78
        result.put("context", ipuContextData == null ? "" : ipuContextData.toString());
79
        result.put("msg", "获取上下文");
64 80
        return result;
65 81
    }
66 82
}

+ 23 - 64
ipu-rest-demo/src/main/java/com/ai/ipu/server/demo/control/data/DataController.java

@ -1,84 +1,43 @@
1 1
package com.ai.ipu.server.demo.control.data;
2 2

3
import java.util.Map;
4

5 3
import org.springframework.stereotype.Controller;
6
import org.springframework.web.bind.annotation.RequestBody;
7 4
import org.springframework.web.bind.annotation.RequestMapping;
8
import org.springframework.web.bind.annotation.RequestParam;
9 5
import org.springframework.web.bind.annotation.ResponseBody;
10 6

11
import com.ai.ipu.basic.util.IpuBaseException;
12 7
import com.ai.ipu.data.JMap;
13 8
import com.ailk.common.data.IData;
14
import com.ailk.common.data.impl.DataMap;
15 9

10
/**
11
 * @author huangbo@asiainfo.com
12
 * @team IPU
13
 * @date 2018年1月30日上午10:11:03
14
 * @desc 数据请求操作范例
15
 */
16 16
@Controller
17
@RequestMapping("/request")
17
@RequestMapping("/data")
18 18
public class DataController {
19 19
	
20
    /**
21
     * @author huangbo@asiainfo.com
22
     * @title: request
23
     * @desc: JMap类型数据请求范例
24
     */
20 25
    @ResponseBody
21
    @RequestMapping("/jget")
22
    public JMap getRequest(JMap params) {
23
        params.put("msg", "测试get请求传参");
24
        return params;
26
    @RequestMapping("/jmap")
27
    public JMap request(JMap param) {
28
        param.put("msg", "JMap请求传参");
29
        return param;
25 30
    }
26 31
    
27
    @ResponseBody
28
    @RequestMapping("/jpost")
29
    public JMap postRequest(JMap params) {
30
        params.put("msg", "测试post请求传参");
31
        return params;
32
    }
33
    
34
	@ResponseBody
35
	@RequestMapping("/get")
36
    public IData getRequest(IData params) {
37
		params.put("msg", "测试get请求传参");
38
        return params;
39
    }
40
	
41
	@ResponseBody
42
	@RequestMapping("/post")
43
    public IData postRequest(IData params) {
44
		params.put("msg", "测试post请求传参");
45
        return params;
46
    }
47
	
48
    @RequestMapping("/error")
49
    public void error() throws Exception {
50
        throw new Exception("使用默认异常抛出全局data异常");
51
    }
52
	
53
    @RequestMapping("/ipuerror")
54
    public void ipuerror() throws Exception {
55
        throw new IpuBaseException("使用IPU异常抛出全局data异常");
56
    }
57
	
58
	@ResponseBody
59
	@RequestMapping("/map")
60
    public Map<String, String> mapRequest(@RequestBody Map<String, String> params) {
61
		params.put("msg", "测试post请求传参");
62
        return params;
63
    }
64
	
65
	@ResponseBody
66
	@RequestMapping("/string")
67
    public String mapRequest(String key) {
68
        return key;
69
    }
70
	
71 32
	/**
72
	 * 通过注解RequestParam获取指定的key的参数
73
	 * @param data
74
	 * @return
33
	 * @author huangbo@asiainfo.com
34
	 * @title: request
35
	 * @desc: IData类型数据请求范例
75 36
	 */
76 37
	@ResponseBody
77
	@RequestMapping(value="/data")
78
    public String stringRequest(@RequestParam(value="data")String data) {
79
		IData params = new DataMap(data);
80
		params.put("msg", "测试get请求传参");
81
		params.put("data", data);
82
        return params.toString();
38
	@RequestMapping("/idata")
39
    public IData request(IData param) {
40
	    param.put("msg", "IData请求传参");
41
        return param;
83 42
    }
84 43
}

+ 60 - 0
ipu-rest-demo/src/main/java/com/ai/ipu/server/demo/control/data/DataOtherController.java

@ -0,0 +1,60 @@
1
package com.ai.ipu.server.demo.control.data;
2

3
import java.util.Map;
4

5
import org.springframework.stereotype.Controller;
6
import org.springframework.web.bind.annotation.RequestBody;
7
import org.springframework.web.bind.annotation.RequestMapping;
8
import org.springframework.web.bind.annotation.RequestParam;
9
import org.springframework.web.bind.annotation.ResponseBody;
10

11
import com.ai.ipu.data.JMap;
12
import com.ai.ipu.data.impl.JsonMap;
13

14
/**
15
 * @author huangbo@asiainfo.com
16
 * @team IPU
17
 * @date 2018年1月31日上午10:11:03
18
 * @desc 数据请求操作范例
19
 */
20
@Controller
21
@RequestMapping("/data")
22
public class DataOtherController {
23
    /**
24
     * @author huangbo@asiainfo.com
25
     * @title: mapRequest
26
     * @desc: Map类型数据请求范例
27
     */
28
    @ResponseBody
29
    @RequestMapping("/map")
30
    public Map<String, String> mapRequest(@RequestBody Map<String, String> params) {
31
        params.put("msg", "测试post请求传参");
32
        return params;
33
    }
34
    
35
    /**
36
     * @author huangbo@asiainfo.com
37
     * @title: stringRequest
38
     * @desc: 字符串类型数据请求范例
39
     */
40
    @ResponseBody
41
    @RequestMapping("/string")
42
    public String stringRequest(String key) {
43
        return key;
44
    }
45
    
46
    /**
47
     * @author huangbo@asiainfo.com
48
     * @title: stringDataRequest
49
     * @desc: 指定字段的字符串类型数据请求范例
50
     * 通过注解RequestParam获取指定的key的参数
51
     */
52
    @ResponseBody
53
    @RequestMapping(value="/data")
54
    public String stringDataRequest(@RequestParam(value="data")String data) {
55
        JMap param = new JsonMap(data);
56
        param.put("msg", "测试get请求传参");
57
        param.put("data", data);
58
        return param.toString();
59
    }
60
}

+ 20 - 8
ipu-rest-demo/src/main/java/com/ai/ipu/server/demo/control/data/SessionController.java

@ -9,23 +9,29 @@ import org.springframework.stereotype.Controller;
9 9
import org.springframework.web.bind.annotation.RequestMapping;
10 10
import org.springframework.web.bind.annotation.ResponseBody;
11 11

12
import com.ai.ipu.data.JMap;
13
import com.ai.ipu.data.impl.JsonMap;
12 14
import com.ai.ipu.restful.web.ServletManager;
13
import com.ailk.common.data.IData;
14
import com.ailk.common.data.impl.DataMap;
15 15

16 16
/**
17 17
 * @author huangbo@asiainfo.com
18 18
 * @team IPU
19 19
 * @date 2018年1月28日下午11:11:46
20
 * @desc Session操作的存取类,默认支持redis
20
 * @desc 会话Session操作范例
21
 * 支持redis
21 22
 */
22 23
@Controller
23 24
@RequestMapping("/session")
24 25
public class SessionController {
25 26
    
27
    /**
28
     * @author huangbo@asiainfo.com
29
     * @title: setSession
30
     * @desc: 设置会话Session的范例
31
     */
26 32
    @ResponseBody
27 33
    @RequestMapping("/set")
28
    public IData setSession(IData param) {
34
    public JMap setSession(JMap param) {
29 35
        // TODO Auto-generated method stub
30 36
        HttpSession session = ServletManager.getSession();
31 37
        Iterator<String> it = param.keySet().iterator();
@ -34,15 +40,21 @@ public class SessionController {
34 40
            key = it.next();
35 41
            session.setAttribute(key, param.get(key));
36 42
        }
37
        IData result = new DataMap();
38
        result.put("msg", "session保存成功");
43
        JMap result = new JsonMap();
44
        result.put("msg", "设置会话Session");
39 45
        return result;
40 46
    }
41 47
    
48
    /**
49
     * @author huangbo@asiainfo.com
50
     * @title: getSession
51
     * @desc: 获得会话Session的范例
52
     */
42 53
    @ResponseBody
43 54
    @RequestMapping("/get")
44
    public IData getSession(IData params) {
45
        IData session = ServletManager.transSession(ServletManager.getRequest().getSession());
55
    public JMap getSession(JMap params) {
56
        JMap session = ServletManager.transSession(ServletManager.getSession());
57
        session.put("msg", "获得会话Session");
46 58
        return session;
47 59
    }
48 60
    

+ 0 - 18
ipu-rest-demo/src/main/resources/ipu-spring-mvc.xml

@ -11,22 +11,4 @@
11 11
		http://www.springframework.org/schema/mvc/spring-mvc.xsd">
12 12
	<!-- 自定义的control扫描目录 -->
13 13
    <context:component-scan base-package="com.ai.ipu.server.demo" />
14
    
15
    <!-- 定义文件上传解析器 -->
16
	<!-- <bean id="multipartResolver"
17
		class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
18
		设定默认编码
19
		<property name="defaultEncoding" value="UTF-8"></property>
20
		设定文件上传的最大值为5MB,5*1024*1024
21
		<property name="maxUploadSize" value="5242880"></property>
22
		设定文件上传时写入内存的最大值,如果小于这个参数不会生成临时文件,默认为10240
23
		<property name="maxInMemorySize" value="40960"></property>
24
		上传文件的临时路径
25
		<property name="uploadTempDir" value="fileUpload/temp"></property>
26
		延迟文件解析
27
		<property name="resolveLazily" value="true" />
28
	</bean> -->
29
    
30
    <!-- 不处理静态资源 -->
31
    <mvc:default-servlet-handler/>
32 14
</beans> 

+ 0 - 19
ipu-rest-demo/src/main/resources/ipu-spring-mvc2.xml

@ -1,19 +0,0 @@
1
<?xml version="1.0" encoding="UTF-8"?>
2
<beans xmlns="http://www.springframework.org/schema/beans"
3
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
5
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
6
           
7
    <!-- 处理器映射器 将bean的name作为url进行查找 -->
8
	<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
9
	<bean name="/name" class="com.ai.ipu.server.demo.control.BeanNameUrlController" />
10
	
11
	<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
12
		<property name="mappings">
13
			<props>
14
				<prop key="/url">urlController</prop>
15
			</props>
16
		</property>
17
	</bean>
18
	<bean id="urlController" class="com.ai.ipu.server.demo.control.UrlController" />
19
</beans>