`

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中

 

	<bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"/>
	<bean  class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">  
         <property name="mappings">  
            <props>  
                  <prop key="/httpService">httpService</prop>  
            </props>  
        </property>  
    </bean>  

 5 ApplicationContext.xml中配置HttpInvoker 

  

<bean id="httpService"  
        class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">  
        <property name="service">  
            <ref bean="ucService" />  
        </property>  
        <property name="serviceInterface"  
            value=" com.mainbo.u3.service.UcService">  
        </property>  
    </bean>  

 6 web.xml中暴漏接口 

 

    <servlet>
        <servlet-name>u3</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>3</load-on-startup>
    </servlet>
	
<servlet-mapping>  
        <servlet-name>u3</servlet-name>  
        <url-pattern>/service/*</url-pattern>  
</servlet-mapping>  

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

  

	<bean id="httpService" 
	    class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean"> 
	    <property name="serviceUrl" value="http://localhost:80/service/httpService" /> 
	    <property name="serviceInterface" value=" com.mainbo.u3.service.UcService" /> 
	</bean>

 

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 );
        
		
	}

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics