WS服务总结

使用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




访问:http://localhost:8080/ws/soap/hello


2,使用CXF内置的Jetty发布WS
MAVEN 添加 cxf依赖

org.apache.cxf>
cxf-rt-frontend-jaxws
3.0.4




org.apache.cxf>
cxf-rt-transports-http-jetty
3.0.4

接口和实现都添加@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


配置spring-cxf.xml





//cxf提供的endpoint方式



//使用simple方式发布ws







//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

   
   


//方法二,使用jaxws:client(推荐)
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)
ListretrieveAllProducts();


@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)
ListretrieveProductByName(@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 fieldMap);


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

ProduceService providerService = JAXRSClientFactory.create(baseAddress,ProductService.class,providerList);
List 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>);
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 providerList = new ArrayList();
productList.add(new JacksonJsonProvider());
List productList = WebClient.create(baseAddress,providerList).path("/products").accept(MediaType.APPLICATION_JSON).get(List.class/new GenericType>);
for(Object product:productList){
System.out.println(product);
}
}
}


使用Spring+CXF发布REST服务
添加Maven依赖
rest_spring_cxf


spring-cxf.xml
xmlns:jaxrs="http://cxf.apache.org/jawrs"
http://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jawrs.xsd










接下来在页面调用即可.
ajax跨域问题
1,jsonp
2,cors(更强大)
安全问题
Spring + CXF + WSS4J
1,基于用户令牌的身份认证
2,基于数字签名的身份认证
3,SOAP消息的加密解密(发送和响应)

打完收工!



















你可能感兴趣的:(spring,架构)