spring httpinvoke使用

 

1 实体,必须序列化

public class UcServiceEntity implements Serializable{   
    /**
	 *  @Fields serialVersionUID : TODO
	 */
	private static final long serialVersionUID = 1L;
	private String name; 
    private String age ;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAge() {
		return age;
	}
	public void setAge(String age) {
		this.age = age;
	} 
} 

 2 服务端与客户端的接口

  

package com.mainbo.u3.service;

import java.util.Map;

public interface UcService {   
    public String getUserInfobyName(String userName);   
    public int recordLog(String username, String point, String operate, String desc);   
    public Map getUserInfobyName(Map userMap);   
    public UcServiceEntity   getUserInfobyName(UcServiceEntity entity);   
}  

 3 服务端接口实现 

  

@Service("ucService")
public class UCServiceImpl implements UcService {   
	  
    private static Logger pointrecordlog = Logger.getLogger("pointrecordlog");   
  
    private static Logger logger = Logger.getLogger(UCServiceImpl.class);   
  
 
  
    public String getUserInfobyName(String userName) {   
        return userName+"12" ;   
    }   
  
    public int recordLog(String username, String point, String operate, String desc) {   
  
        int result = 0;   
        try {   
            pointrecordlog.info(username + " - " + point + " - " + operate + " - " + desc);   
        } catch (Throwable t) {   
            result = -1;   
            logger.error(t);   
        }   
        return result;   
  
    }

	@Override
	public Map getUserInfobyName(Map userMap) {
		System.out.println( userMap);
		Map map  = new HashMap();
		map.put("userName", "王战涛");
		return map;
	}

	
	@Override
	public UcServiceEntity getUserInfobyName(UcServiceEntity entity) {
		 System.out.println( entity.getName());
		 entity.setName("getUserInfobyName");
		return entity;
	}   
  
}  

 4 映射mapping ,在springmvc 的配置文件中*-servlet.xml中

 

	
	  
           
              
                  httpService  
              
          
      

 5 ApplicationContext.xml中配置HttpInvoker 

  

  
          
              
          
          
          
      

 6 web.xml中暴漏接口 

 

    
        u3
        org.springframework.web.servlet.DispatcherServlet
        3
    
	
  
        u3  
        /service/*  
  

 7 客户端配置ApplicationContext.xml ,暴漏访问url

  

	 
	     
	     
	

 

8 主函数进行测试

 

	public static void main(String[] args) {
		
 
		
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
                new String[] {"u3-core.xml" });
    
        UcService us = (UcService)ctx.getBean("httpService");
         String userName = us.getUserInfobyName("111111");
         System.out.println( userName );
         
 	Map map  = new HashMap();
 	map.put("userName", "王涛1");
        Map mapUser =  us.getUserInfobyName(map );
        System.out.println(mapUser.get("userName") );
        
        UcServiceEntity entity = new UcServiceEntity(); 
        entity.setName("传递实体");
        UcServiceEntity entity2 = us.getUserInfobyName(entity);
        System.out.println(entity2 );
        
		
	}

 

 

你可能感兴趣的:(spring)