thrift 实例开发

1JjCases.thrift

namespace java cn.focus.dc.jiajing.model.thrift

struct JjCases{
1: i32 id,
2: i32 userId,
3: string title,
4: string description,
5: i32 createTime,
6: i32 updateTime,
7: string status
}


service JjCasesService{


JjCases get(1:i32 id),

i32 add(1:JjCases jjCases),

i32 update(1:JjCases jjCases),

i32 remove(1:i32 id)

i32 updateStatusByIds(1:i32 status, 2:string ids)

list<JjCases> getOrderListByStatus(1:i32 status, 2:i32 userId, 3:i32 offset, 4:i32 limit)
}


2、thrift-0.9.1.exe --gen java JjCases.thrift
生成文件
JjCases.java 和 JjCasesService.java

cn.focus.dc.jiajing.model.thrift

3、cn.focus.dc.jiajing.service.thrift 下编写service类

package cn.focus.dc.jiajing.service.thrift;

import java.util.List;

import org.apache.log4j.Logger;
import org.apache.thrift.TException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import cn.focus.dc.jiajing.dao.JjCasesDAO;
import cn.focus.dc.jiajing.model.thrift.JjCases;
import cn.focus.dc.jiajing.model.thrift.JjCasesService;

/**
* @author qiaowang
*/
@Service
public class JjCasesThriftService implements JjCasesService.Iface {

private static final Logger logger = Logger.getLogger(JjCasesThriftService.class);

@Autowired
private JjCasesDAO jjCasesDAO;

@Override
public JjCases get(int id) throws TException {
cn.focus.dc.jiajing.model.JjCases jjCases = new cn.focus.dc.jiajing.model.JjCases();
JjCases thriftJjCases = new JjCases();
try {
if (id > 0) {
jjCases = jjCasesDAO.get(id);
BeanUtils.copyProperties(jjCases, thriftJjCases);
}
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
return thriftJjCases;
}

@Override
public int add(JjCases jjCases) throws TException {
// TODO Auto-generated method stub
return 0;
}

@Override
public int update(JjCases jjCases) throws TException {
// TODO Auto-generated method stub
return 0;
}

@Override
public int remove(int id) throws TException {
// TODO Auto-generated method stub
return 0;
}

@Override
public int updateStatusByIds(int status, String ids) throws TException {
// TODO Auto-generated method stub
return 0;
}

@Override
public List<JjCases> getOrderListByStatus(int status, int userId, int offset, int limit) throws TException {
// TODO Auto-generated method stub
return null;
}

}



4、配置文件 才用http invoke
参考
http://blog.csdn.net/liaq325/article/details/8281550


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
">

<bean name="/groupService.do"
class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
<property name="serviceInterface" value="cn.focus.dc.jiajing.model.thrift.GourpManager$Iface"></property>
<property name="service" ref="groupService"></property>
</bean>

<bean name="/JjCasesThriftService.do"
class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
<-接口->
<property name="serviceInterface" value="cn.focus.dc.jiajing.model.thrift.JjCasesService$Iface"></property>
<实现>
<property name="service" ref="JjCasesThriftService"></property>
</bean>

</beans>



5、客户端调用
配置:

<bean id="jjCasesThriftService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
<property name="serviceUrl">
<value>http://localhost:8083/JjCasesThriftService.do</value>
</property>
<property name="serviceInterface">
<value>cn.focus.dc.jiajing.model.thrift.JjCasesService$Iface</value>
</property>
</bean>

controller类:

package cn.focus.dc.jiajing.controllers;
import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import cn.focus.dc.config.MsgConstant;
import cn.focus.dc.jiajing.model.thrift.JjCases;
import cn.focus.dc.jiajing.model.thrift.JjCasesService;
import cn.focus.dc.jiajing.util.JsonResponse;

/**
* @author qiaowang
*
*/
@Path("test")
public class TestController {

private static Logger logger = Logger.getLogger(TestController.class);

@Autowired
private JjCasesService.Iface jjCasesThriftService;

@Get("get")
public String get(@Param("id") int id) {
try {
cn.focus.dc.jiajing.model.JjCases jjCases = new cn.focus.dc.jiajing.model.JjCases();
if (id > 0) {
JjCases thriftJjCases = jjCasesThriftService.get(id);
BeanUtils.copyProperties(thriftJjCases, jjCases);
return JsonResponse.ok(jjCases);
}
return JsonResponse.badResult(MsgConstant.PARAM_ERROR);
} catch (Exception e) {
logger.error(e.getMessage(), e);
return JsonResponse.badResult(MsgConstant.SERVER_ERROR);
}
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值