• [Cxf] cxf 相关知识整理


    ① 请求方式为GET

       @GET
        @Path(value = "/userAddressManage")
        @Produces( { MediaType.APPLICATION_JSON, "text/html; charset=UTF-8" })
        public BuyerAddressResponseModel userAddressManage(
                @QueryParam("buyerTel") String buyerTel,
                  @QueryParam("detailAddress") String detailAddress);

     和POST请求方式对应的获取参数数据的方式是: @QueryParam

    ② 请求方式为POST

    通过GET的方式来提交数据,参数的长度是都限制的,当数据的长度超过了限制,请求会失败。

    这种情况下就不得不使用POST请求了。

        @POST
        @Path(value = "/addOrder")
        @Produces( { MediaType.APPLICATION_JSON, "text/html; charset=UTF-8" })
        public OrderResponseModel addOrder(
                @FormParam("orderInfo") String orderInfo,
                @FormParam("orderDetailsList") String orderDetailsList,
                @FormParam("addressInfo") String addressInfo);

     和POST请求方式对应的获取参数数据的方式是: @FormParam

    ③ CXF相关注解整理

    1)@WebService 和 @WebMethod 是 WSDL 映射 Annatotion。描述 Web Service 的 WSDL 文档元素和 Java 源代码联系在一起。

    2)@WebParam 用来获取请求参数

    3)@WebResult用来定义返回值

    4)@SOAPBinding 是一个绑定的 annotation 用来说明网络协议和格式。 

    使用annotation 定义了webservice

    import java.util.List;
    import javax.jws.WebMethod;
    import javax.jws.WebParam;
    import javax.jws.WebResult;
    import javax.jws.WebService;
    import com.cxf.pojo.User;
    
    @WebService(targetNamespace = "http://jdk.study.hermit.org/client")
    public interface UserService {
        @WebMethod(operationName = "Insert")
        public void insert(
           @WebParam(name = "userId") String userid, @WebParam(name = "userName") String username, @WebParam(name = "userEmail") String useremail, @WebParam(name = "userAge") int userage); @WebMethod(operationName = "GetUserById") @WebResult(name = "result") public User getUserById(@WebParam(name = "userid") String userid); @WebMethod(operationName = "GetAllUsers") @WebResult(name = "result") public List getAllUsers(); }

    实现类如下:

    import  java.util.List; 
    import  javax.jws.WebService; 
    
    import  com.cxf.dao.UserDao; 
    import  com.cxf.pojo.User; 
    import  com.cxf.service.UserService; 
    
    @WebService (endpointInterface= "com.cxf.service.UserService" ) 
    publicclass UserServiceImpl  implements  UserService { 
    
      private UserDao  userDao ; 
      public List getAllUsers() { return userDao .findAllUser(); } 
      public User  getUserById(String userid) { return userDao .findUserById(userid);} 
    
      publicvoid insert(String userid, String username, String useremail,  int  userage) { 
        User user= new  User(); 
        user.setUserage(userage); 
        user.setUseremail(useremail); 
        user.setUserid(userid); 
        user.setUsername(username); 
        userDao .insert(user); 
        System. out .println( "insert successfully!" ); 
      } 
    
      public   void  setUserDao(UserDao userDao) { 
        this . userDao  = userDao; 
      } 
    } 
    注意:实现类中的@WebService ,其中的 endpointInterface 成员指定了该类实现的接口

    下面介绍restful在springMVC下的注解

      
    Annotation 注解在 javax.ws.rs.* 中定义,是 JAX-RS (JSR 311) 规范的一部分。
    @Path       定义资源基 URI。由上下文根和主机名组成,资源标识符类似于 http://localhost:8080/RESTful/rest/hello 在类和方法上使用
    @GET      这意味着以下方法可以响应 HTTP GET 方法
    @Produces   以纯文本方式定义响应内容 MIME 类型
    @Context    使用该注释注入上下文对象,比如 Request、Response、UriInfo、ServletContext
    @FormParam  接收POST方式提交的参数
    @QueryParam 接收GET方式提交的参数
    @Consumes    声明该方法使用 HTML FORM
    @Produces( { MediaType.APPLICATION_JSON, "text/html; charset=UTF-8" })

    定义REST接口服务

    package demo.ws.rest_cxf;
     
    import java.util.List;
    import java.util.Map;
    import javax.ws.rs.Consumes;
    import javax.ws.rs.DELETE;
    import javax.ws.rs.FormParam;
    import javax.ws.rs.GET;
    import javax.ws.rs.POST;
    import javax.ws.rs.PUT;
    import javax.ws.rs.Path;
    import javax.ws.rs.PathParam;
    import javax.ws.rs.Produces;
    import javax.ws.rs.core.MediaType;
     
    public interface ProductService {
     
        @GET
        @Path("/products")
        @Produces(MediaType.APPLICATION_JSON)
        List<Product> retrieveAllProducts();
     
        @GET
        @Path("/product/{id}")
        @Produces(MediaType.APPLICATION_JSON)
        Product retrieveProductById(@PathParam("id") long id);
     
        @POST
        @Path("/products")
        @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
        @Produces(MediaType.APPLICATION_JSON)
        List<Product> retrieveProductsByName(@FormParam("name") String name);
     
        @POST
        @Path("/product")
        @Consumes(MediaType.APPLICATION_JSON)
        @Produces(MediaType.APPLICATION_JSON)
        Product createProduct(Product product);
     
        @PUT
        @Path("/product/{id}")
        @Consumes(MediaType.APPLICATION_JSON)
        @Produces(MediaType.APPLICATION_JSON)
        Product updateProductById(@PathParam("id") long id, Map<String, Object> fieldMap);
     
        @DELETE
        @Path("/product/{id}")
        @Produces(MediaType.APPLICATION_JSON)
        Product deleteProductById(@PathParam("id") long id);
    }

    以上 ProductService 接口中提供了一系列的方法,在每个方法上都使用了 JAX-RS 提供的注解,主要包括以下三类:

    1. 请求方式注解,包括:@GET、@POST、@PUT、@DELETE
    2. 请求路径注解,包括:@Path ,其中包括一个路径参数
    3. 数据格式注解,包括:@Consumes(输入)、@Produces(输出),可使用 MediaType 常量
    4. 相关参数注解,包括:@PathParam(路径参数)、@FormParam(表单参数),此外还有 @QueryParam(请求参数)

    针对 updateProductById 方法,简单解释一下:

    该方法将被 PUT:/product/{id} 请求来调用,请求路径中的 id 参数将映射到 long id 参数上,请求体中的数据将自动转换为 JSON 格式并映射到 Map<String, Object> fieldMap 参数上,返回的 Product 类型的数据将自动转换为 JSON 格式并返回到客户端。

    参考网址:

    http://my.oschina.net/huangyong/blog/294324

    http://www.cnblogs.com/hoojo/archive/2012/07/23/2605219.html

  • 相关阅读:
    【书目20200917】影响力思维
    Spring AOP +自定义注解 + Spel表达式 实现审计日志
    SPRING BOOT 注解之OBJECTPROVIDER源码追踪
    Activiti~相关概念
    ELK~fluentd多行日志的收集
    k8s~向etc/hosts里添加内容
    maven~本地仓库的指定
    ssh~ Access denied问题解决
    MySQL如何绕过授予information_schema中对象时报ERROR 1044(4200)错误
    Zabbix如何监控SQL Server服务状态
  • 原文地址:https://www.cnblogs.com/avivaye/p/4195953.html
Copyright © 2020-2023  润新知