rest服务例子

1、接口类(IHello)


import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;

import com.bean.Result;

@Path("/find/")
public interface IHello {

/**
* 登录
* @param userName 帐号
* @param userPass 密码
* @return
*/
@POST
@Path("/login")
Result login(@FormParam("userName")String userName,@FormParam("userPass")String userPass);

/**
* 按名称查询
* @param who 名字
* @return
*/
@GET
@Path("/findByName/{who}")
Result findByName(@PathParam("who")String who);

}


2、实现类(HelloImpl)

public class HelloImpl implements IHello {

@Override
public Result findByName(String who) {
//逻辑实现
}

@Override
public Result login(String userName, String userPass) {
//逻辑实现
}

}


3、返回实体(Result)

import java.util.List;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "result")
public class Result implements Serializable{

/**
* 返回结果
*/
private List<User> lstUser;

public List<User> getLstUser() {
return lstUser;
}

public void setLstUser(List<User> lstUser) {
this.lstUser = lstUser;
}
}



4、app-rest.xml配置

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.xsd">


<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

<jaxrs:server id="hello" address="/hello">
<jaxrs:extensionMappings>
<entry key="json" value="application/json" />
<entry key="xml" value="application/xml" />
</jaxrs:extensionMappings>
<jaxrs:serviceBeans>
<ref bean="helloImpl" />
</jaxrs:serviceBeans>
</jaxrs:server>

<bean id="helloImpl" class="com.HelloImpl" />
</beans>


5、依赖jar包

<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>apache-cxf</artifactId>
<version>2.2.7</version>
<type>pom</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>jsr311-api</artifactId>
<version>1.0</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.5.4</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>2.5.4</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<type>jar</type>
<scope>provided</scope>
</dependency>


6、web.xml配置

<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>


7、调用方法
方法一:

HttpClient httpClient = new HttpClient();
GetMethod getMethod = new GetMethod("http://localhost:8080/Test/rest/hello/find/findByName/jack.xml");
try {
int status = httpClient.executeMethod(getMethod);
if (status == 200) {
XMLSource xmlSource = new XMLSource(getMethod.getResponseBodyAsStream());
Result result = xmlSource.getNode("/", Result.class);
System.out.println(result.getLstUser().size());
}
} catch (Exception e) {
e.printStackTrace();
}

方法二:

IHello hello = (IHello) JAXRSClientFactory.create("http://localhost:8080/Test/rest/hello", IHello.class);
Result result = hello.findByName("jack");
System.out.println(result.getLstUser().size());


8、优缺点和注意事项
1.优缺点
优点:跨平台(支持xml和json格式)
缺点:效率低
2.注意事项
参数:如果不是java的基本类型需要封装成javabean,类必须加如下注解:

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Param {
...
}

返回值:如果不是java基本类型需要封装成javabean,需要序列化,属性和类需要加如下注解:

@XmlRootElement(name = "result")
public class Result implements Serializable {
@Field
private String id;
...
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用方法: 1、下载工程文件; 2、将工程文件导入到eclipse等,或者直接进行发布,如直接将工程放到【tomcat】/webapps/下即可。 3、搜索整个工程文件,将字符串8080改为你自己服务器的IP; 4、启动服务器。 测试方法: 1、main函数测试 打开Client类,运行main函数可测试post和delete方法; 2、进行单个Application测试: 1)返回所有学生信息:http://localhost:8080/RestApplication/rest/student 2)执行Client类的main函数,添加一条学生信息; 3)重复1); 4)获取ID=1的单个学生信息:http://localhost:8080/RestApplication/rest/student/1 5)打开http://localhost:8080/RestApplication/update.jsp页面,输入相关信息,提交;如果提交失败,请检查页面表单的 action属性值是否正确; 6)重复4),查看信息被修改的情况; 3、使用Component绑定多个Application 本代码源自转自【http://www.lifeba.org/arch/restlet_develop_application_component_2.html 】但有改动,主要改动有: 1. 修改了web.xml的段,使工程既可以访问rest服务,又可以访问普通的页面资源,不用再像原作者那样再单独部署一个页面工程。 2. 由于【1】的改动,使得只有以/rest开头的URL才能映射到某资源,使用rest服务时,必须要加上/rest。 3. 由于【1】的改动,RestComponent类注册application时将资源字符串加上了/rest。 4. 由于【1】的改动和本人WEB服务器端口号的不同,Client测试类的相关资源字符串也做了相应改动。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值