由于工作需要,研究了一下Rest.
1.创建web应用,加入如下jar包。
2.创建Student实体
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="student") public class Student { private int id; private String name; public Student(){}
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
@Override public String toString() { return "Student [id=" + id + ", name=" + name + "]"; }
|
3.创建Resource
import java.util.Iterator;
import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces;
import org.codehaus.jackson.map.deser.FromStringDeserializer; import org.restlet.data.Form; import org.restlet.data.Parameter; import org.restlet.representation.Representation;
@Path("student") public class StudentResource { @GET @Path("{id}/xml") @Produces("application/xml") public Student getStudentXml(@PathParam("id") int id) {
//路径为http://localhost:8085/student/1/xml return ResourceServer.getDefaultStudent(); } @GET @Path("{id}/json") @Produces("application/json") public Student getStudentJson(@PathParam("id") int id) {
//路径为http://localhost:8085/student/1/json return ResourceServer.findStudent(id); } @GET @Path("delete/{name}") public void delete(@PathParam("name") String name){
//路径为http://localhost:8085/student/delete/小王 System.out.println("和数据库交互,删除name 为"+name +"的student"); } @POST @Path("update") public void update(Representation data){ //form表单通过Post将数据传过来。路径为http://localhost:8085/student/update/+form数据 Form form =new Form(data); Iterator<Parameter> it =form.iterator(); Student stu =new Student(); while(it .hasNext()){ Parameter p =it.next(); String key =p.getName(); String val =p.getValue(); if("id".equals(key)){ stu.setId(Integer.parseInt(val)); }else if("name".equals(key)) { stu.setName(val); } } // 将student加入到数据库。 }
}
|
4 .创建Application
public class StudentApplication extends Application {
@Override public Set<Class<?>> getClasses() { Set<Class<?>> rrcs = new HashSet<Class<?>>(); rrcs.add(StudentResource.class); return rrcs; } }
|
5 创建Server 类,该类启动rest服务。
public class Server { public static void main(String[] args) throws Exception { Component component = new Component(); component.getServers().add(Protocol.HTTP, 8085); component.getDefaultHost().attach(new StudentApplication (new Context())); component.start(); } } |
启动该类,rest服务就被绑定在了8085端口。
<!--StartFragment -->
http://localhost:8085/student/1/json
其他功能以此类推。
实例项目提供下载。
相关推荐
restful入门实例(可运行) 框架:rest,struts2.0,hibernate,spring
Java restful和webservice接口, WebService有两种方式,一是SOAP方式,二是REST方式。SOAP是基于XML的交互,WSDL也是一个XML文档,可以使用WSDL作为SOAP的描述文件;REST是基于HTTP协议的交互,支持JSON、XML等交互...
RESTful架构实战
《Java RESTful Web Service实战》PDF版本下载
Java RESTful Web Service实战.pdf Java RESTful Web Service实战.pdf
Java+restful+Web+Service实战,完整版,非常值得拥有。
主要介绍了Python中Flask-RESTful编写API接口(小白入门),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
restful web service 实战 第二版 源码
项目概述:本项目是一个基于Python语言的实战项目,使用了Flask框架构建RESTful API。它集成了Flask-SQLAlchemy作为ORM工具与MySQL数据库进行交互。项目共包含39个文件,其中主要的Python脚本文件有23个,辅助配置...
RESTful Web Services 中文版 高清 PDF 电子书
《Java Restful Web Service实战》源代码
C# 一个简单的 Restful 服务端和 Restful 客户端 Demo
springboot入门demo...实现了restful api和webservice两种接口方式。(可通过postman或soupui进行测试。) 同时包含了Junit测试webservice。 Demo是经过亲测完全可以跑起来。通过此demo可对springboot有个入门的理解
CXF发布restful WebService的入门例子.pdfCXF发布restful WebService的入门例子.pdf
restful接口示例代码restful接口示例代码restful接口示例代码restful接口示例代码
restful WebService的入门例子.pdfrestful WebService的入门例子.pdf
1-Java RESTful Web Service实战.part1.rar 完整版第一部分
GORSK-GO(lang)Restful入门套件 Gorsk-Gin是用于使用Gin框架开发RESTful服务的Golang入门套件。 我最初使用Gin开发 ,但后来将其移至Echo。 该存储库将通过在Gorsk上进行的较小更改来维护。 它旨在帮助您启动...
GORSK-GO(lang)Restful入门套件 Gorsk是用于开发RESTful服务的Golang入门套件。 它旨在帮助您启动项目,跳过“设置部分”,直接跳转到编写业务逻辑。 以前,Gorsk是使用建造的。 使用Gin的Gorsk在。 Gorsk遵循...