RPC 包含的要素
webservice
协议层:tcp/ip
应用层: http协议
SOAP: http+xml
webservice也可以叫xml web service webservice, 轻量级的独立的通讯技术
跨语言调用的解决方案
电商平台,订单的物流状态。
.net实现的webservice服务接口
webservice服务需要通过wsdl文件来说明自己有什么服务可以对外调用。并且有哪些方法、方法里面有哪些参数
wsdl基于XML(可扩展标记语言)去定义的
http+xml
webservice通过http协议发送和接收请求时, 发送的内容(请求报文)和接收的内容(响应报文)都是采用xml格式进行封装
这些特定的HTTP消息头和XML内容格式就是SOAP协议
webservice服务端用来处理请求的接口,也就是发布出去的接口。
略
pom.xml
4.0.0
com.gupao.vip.mic.webservice
webservice-restful
war
1.0-SNAPSHOT
webservice-restful Maven Webapp
http://maven.apache.org
4.3.10.RELEASE
1.9.2
junit
junit
3.8.1
test
org.springframework
spring-core
${spring.version}
org.springframework
spring-beans
${spring.version}
org.springframework
spring-context
${spring.version}
org.springframework
spring-context-support
${spring.version}
org.springframework
spring-web
${spring.version}
org.apache.cxf
cxf-rt-transports-http
3.1.8
org.apache.cxf
cxf-rt-frontend-jaxws
3.1.8
org.apache.cxf
cxf-rt-frontend-jaxrs
3.1.8
javax.ws.rs
javax.ws.rs-api
2.0
javax.ws.rs
jsr311-api
1.1.1
org.codehaus.jackson
jackson-core-asl
${jackson.version}
org.codehaus.jackson
jackson-mapper-asl
${jackson.version}
org.codehaus.jackson
jackson-jaxrs
${jackson.version}
org.codehaus.jackson
jackson-xc
${jackson.version}
webservice-restful
web.xml
Archetype Created Web Application
contextConfigLocation
classpath:application.xml
org.springframework.web.context.ContextLoaderListener
CXFServlet
org.apache.cxf.transport.servlet.CXFServlet
1
CXFServlet
/ws/*
public class Response {
String code;
String msg;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
import java.util.Arrays;
import java.util.List;
public class Storage {
//数据仓库
public static List users= Arrays.asList(
new User(1,"james"),
new User(2,"mic"),
new User(3,"sam")
);
}
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class User {
private int id;
private String name;
public User() {
}
public User(int id, String name) {
this.id = id;
this.name = name;
}
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 "User{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
import javax.jws.WebService;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.List;
@WebService
@Path(value="/users/")
public interface UserService {
@GET
@Path("/") //http://ip:port/users
@Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
List getUsers();
@DELETE
@Path("{id}") //http://ip:port/users/1
@Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) //请求accept
Response delete(@PathParam("id") int id);
@GET
@Path("{id}") //http://ip:port/users/1
@Produces(MediaType.APPLICATION_JSON)
User getUser(@PathParam("id") int id);
@POST
@Path("add")
Response insert(User user);
@PUT
@Path("update")
Response update(User user);
}
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService{
public List getUsers() {
return Storage.users;
}
public Response delete(int id) { //0 1
// Storage.users.remove(id);
Response response=new Response();
response.setCode("00");
response.setMsg("succes");
return response;
}
public User getUser(int id) {
return Storage.users.get(id);
}
public Response insert(User user) {
return null;
}
public Response update(User user) {
return null;
}
}
application.xml