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

你可能感兴趣的:(java,apache,spring,xml,REST)