如何实现ssm整合下的controller接口(用于整合Vue的步骤之一)

实现ssm整合下的controller接口(用于整合Vue的步骤之一)

1.修改pom文件,添加异步通信的依赖

2.修改spring配置文件

3.在controller的方法内添加注解@ResponseBody 

//这个是会返回json的

 

1.pom文件如下 

 
    
      com.fasterxml.jackson.core
      jackson-databind
      2.7.4
    
    
      com.fasterxml.jackson.core
      jackson-core
      2.7.4
    
    
      com.fasterxml.jackson.core
      jackson-annotations
      2.7.4
    

 

2.spring配置文件内要添加的内容



    
        
            
                
                
            
        
    
    
        
            
                
            
        
    

    
        
            
                
                    
                    
                    
                
                
                    
                    
                    
                
                
                    
                    
                    
                
                
                    
                    
                    
                
            
        
    

3.具体例子 

(controller内怎么写)

@RequestMapping(value = "getJson", method = RequestMethod.GET)
	@ResponseBody
	public Map getJson(HttpSession httpSession){
		Map map = new HashMap();
		List cs = new LinkedList();
		cs.add(new Category());
		cs.add(new Category());
		cs.add(new Category());
		cs.add(new Category());
		try {
			map.put("errorCode", 0);
			map.put("message", "hello");
			map.put("cs",cs);
		} catch (Exception e) {
			map.put("errorCode", 1);
			map.put("errorMessage", "未知错误");
		}
		System.out.println("已经进入controllor");
		System.out.println("已经设置返回了");
		return map;

	}

 

最后,前后端分离还会涉及到跨域问题,还需要前端做请求拦截并且转换或者后台做过滤处理。

你可能感兴趣的:(后端随笔)