WS服务总结

来源:互联网 发布:单片机 容易发的sci 编辑:程序博客网 时间:2024/06/04 18:36
使用CXF开发SOAP服务
1,使用RI发布WS
只需解压下载的jax-ws.java.net/2.2.8/即可
编写WS接口及其实现
接口添加 @WebService 注解
实现添加 @WebService(serviceName = "", protName = "", endpointInterface = "demo.ws.soap_jaxws.HelloService") 注解.
然后在WEB-INF下添加sun-jaxws.xml
<?xml version="1.0" encoding="UTF-8"?>
<endpoints xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime" version="2.0">
<endpoint name = "HelloService" implementation="demo.ws.soap_jaxws.HelloService" url-pattern="/ws/soap/hello"/>
</endpoints>
访问:http://localhost:8080/ws/soap/hello


2,使用CXF内置的Jetty发布WS
MAVEN 添加 cxf依赖
<dependency>
<groupId>org.apache.cxf</<groupId>>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.0.4</version>
</dependency>


<dependency>
<groupId>org.apache.cxf</<groupId>>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>3.0.4</version>
</dependency>
接口和实现都添加@WebService注解
public class JaxWsService{
public stitac void main(String[]args){
JaxWsServiceFactoryBean factory = new JaxWsServiceFactoryBean();
factory.setAddress("http://localhost:8080/ws/soap/hello");
factory.setServiceClass(HelloService.class);
factory.setServiceBean(new HelloServiceImpl());
factory.create();
System.out.println("soap ws is published")
}
simple方式:(只能通过客户端调用)
public class SimpleService{
public stitac void main(String[]args){
ServiceFactoryBean factory = new ServiceFactoryBean();
factory.setAddress("http://localhost:8080/ws/soap/hello");
factory.setServiceClass(HelloService.class);
factory.setServiceBean(new HelloServiceImpl());
factory.create();
System.out.println("soap ws is published")
}


Sping + CXF 发布WS
添加cxf依赖,添加spring依赖
接口添加@WebService,实现类添加@WebService和@Component注解
添加web.xml servlet指向cxf
org.apache.cxf.transport.servlet.CXFServlet
配置spring xml
<context:component-scan base-package="demo.ws"/>
<import resource="spring-cxf.xml"/>
配置spring-cxf.xml
<jawxs:service id="helloService" address="/soap/hello">
<jaxws:serviceBean>
<ref bean="helloServiceImpl"/>
</jaxws:serviceBean>
</jawxs:service>
//cxf提供的endpoint方式
<jaws:endpoint id="helloService" implementor="#helloServiceImpl" address="/soap/hello"/>


//使用simple方式发布ws
<simple:server id="helloService" serviceClass="#helloService" address="/soap/hello">
<simple:serviceBean>
<ref bean="helloServiceImpl"/>
</simple:serviceBean>
</simple:service>


//CXF提供的WS客户端
//方案一,静态代理客户端
public class JaxWsClient{
    public static void main(String[]args){
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        factory.setAddress("http://localhost:8080/ws/soap/hello");
        factory.setServiceClass(HelloSerive.clss);
        HelloService helloService = factory.create(HelloService.class);
        String result = helloService.say("world");
        System.out.println(result);
    }
}
//方案二,动态代理客户端
public class JaxWsDynamicClient{
    public static void main(String[]args){
        JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();
        Client client = factory.createClient("http://localhost:8080/ws/soap/hello?wsdl");
        try{
            Object [] result = client.invoke("say","world");
            System.out.println(result[0]);
        }catch (Exceptioin e){
            e.pringtStackTrace();
        }
    }
}
//方案三,通用动态代理客户端
public class DynamicClient{
    public static void main(String[]args){
        DynamicClientFactory factory = DynamicClientFactory.newInstance();
        Client client = factory.createClient("http://localhost:8080/ws/soap/hello?wsdl");
        try {
            Object[] result = client.invoke("say","world");
            System.out.println(result[0]);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
//方案四,基于CXF simple
public class SimpleClient{
    public static void main(String[]args){
        ClientProxyFactoryBean factory = new ClientProxyFactoryBean();
        factory.setAddress("http://localhost:8080/ws/soap/hello");
        factory.setServiceClass(HelloService.class);
        HelloService helloService = factory.create(HelloService.class);
        String result = helloService.say("world");
        Sytem.out.println(result);
    }
}
//方案五,基于Spring的客户端
//方法一,使用JaxWsProxyFactoryBean
<bean id="factoryBean" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
    <property name="serviceClass" value="demo.ws.soap_spring_spring_cxf.HelloService"/>
    <property name="address" value="http://localhost:8080/ws/soap/hello"/>
</bean>
<bean id="helloService" factory-bean="factoryBean" factory-method="create"/>
//方法二,使用jaxws:client(推荐)
<jawxs:client id="helloService" serviceClass="demo.ws.soap_spring_cxf.HelloService"
address="http://localhost:8080/ws/soap/hello"/>
//客户端代码
public class Client{
    public static void main(String[]args){
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-client.xml");
        HelloService helloService = context.getBean("helloService",HelloService.class);
        String result = helloService.say("world");
        System.out.println(result);
    }
}


使用CXF开发REST服务
jax-rs 规范
Jersey
Restlet
RESTEasy
CXF
1,添加cxf依赖
cxf-rt-frontend-jaxrs
cxf-rt-transports-http-jetty
jackson-jawxs-json-provider
2,定义rest接口
@GET
@Path("/products")
@Produces(MediaType.APPLICATION_JSON)
List<Product>retrieveAllProducts();


@GET
@Path("/product/{id}")
@Produces(MediaType.APPLICATION_JSON)
Product retrieveProductById(@Path("id") long id);


@POST
@Path("/products")
@Comstoms(MediaType.APPLICATION_FORM_URLENCODE)
@Produces(MediaType.APPLICATION_JSON)
List<Product>retrieveProductByName(@FromParam("name") String name);


@POST
@Path("/product")
@Comstoms(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
Product createProduct(Product product);


@PUT
@Path(/product/{id})
@Comstoms(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
Product upateProductById(@PathParam("id") long id,Map<String,Object> fieldMap);


@Delete
@Path(/product/{id})
@Produces(MediaType.APPLICATION_JSON)
Product deleteProductById(@PathParam("id") long id);
数据格式
@Comstoms(输入)
@Produces(输出)
@QueryParam("请求参数")
3,发布rest服务
//添加ResourceClass
List<Class<?>> resourceClassList = new ArrayList<Class<?>>();
resourceClassList.add(ProduceServiceImpl.class);
//添加ResourceProvider
List<ResouceProvider> resourceProviderList = new ArrayList<resourceProvider>();
resourceProviderList.add(new SingletonResourceProvider(new ProduceServiceImpl()));
//添加Provider
List<Object>providerList = new ArrayList<Object>();
providerList.add(new JacksonJsonProvider());


JAXRSServerFactoryBean factory = new JAXRSServerFactoryBean();
factory.setAddress("http://localhost:8080/ws/rest");
factory.setResourceClasses(resourceClassList);
factory.setResourceProviders(resourceProviderList);
factory.setProviders(providerList);
factory.create();
4,使用cxf调用REST服务
添加依赖
cxf-rt-rs-client
第一种jax-rs 1.0
public class JAXRSClient{
public static void main(String[]args){
String baseAddress = "http://localhost:8080/ws/rest";
List<Object> providerList = new ArrayList<Object>();
providerList.add(new JacksonJsonProvider());

ProduceService providerService = JAXRSClientFactory.create(baseAddress,ProductService.class,providerList);
List<Product> productList = productService.retrieveAllProducts();
for(Product product : productList){
System.out.println(product);
}
}
}


第二种,2.0
public class JAXRS20Client{
public static void main(String[]args){
String baseAddress = "http://localhost:8080/ws/rest";
JacksonJsonProvider jsonprovider = new JacksonJsonProvider();
List productList = ClientBuilder.newClient().register(jsonprovider).target(baseAddress).path("/products").request(MediaType.APPLICATION_JSON).get(List.class/new GenericType<List<Product>>);
for(Object product:productList){
System.out.println(product);
}
}
}


第三种,通用的WebClient客户端
public class CXFWebClient{
public static void main(String[]args){
String baseAddress = "http://localhost:8080/ws/rest";
List<Object> providerList = new ArrayList<Object>();
productList.add(new JacksonJsonProvider());
List productList = WebClient.create(baseAddress,providerList).path("/products").accept(MediaType.APPLICATION_JSON).get(List.class/new GenericType<List<Product>>);
for(Object product:productList){
System.out.println(product);
}
}
}


使用Spring+CXF发布REST服务
添加Maven依赖
rest_spring_cxf
<context:component-scan base-package="demo.ws"/>
<import resource="spring-cxf.xml"/>
spring-cxf.xml
xmlns:jaxrs="http://cxf.apache.org/jawrs"
http://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jawrs.xsd


<jaxrs:server address="/rest">
<jaxrs:serviceBeans>
<ref bean="productServiceImpl"/>
</jawrs:serviceBeans>
<jaxrs:providers>
<jaxrs class="com.fasterxml.jackson.jawxs.json.JacksonJsonProvider"/>
<jawrs:providers>
</jawrs:server>
接下来在页面调用即可.
ajax跨域问题
1,jsonp
2,cors(更强大)
安全问题
Spring + CXF + WSS4J
1,基于用户令牌的身份认证
2,基于数字签名的身份认证
3,SOAP消息的加密解密(发送和响应)

打完收工!