书写webservice接口

1.书写Service       webservice注解java

package com.weichai.modules.webservice.service;

import javax.jws.WebService;

/**
 * 实验结果分析webservice
 * @author ltx
 * @version 2017-11-13
 */
@WebService
public interface AnalysisResultService {

    /**
     * 给分析系统调用接口
     * @param id
     * @param imgSuffix
     * @param imgStr
     * @return
     */
    String getTaskResultInfo(String id,String imgSuffix ,String imgStr);

}

2.书写imp实现类   webservice注解web

package com.weichai.modules.webservice.service.impl;

import com.weichai.common.config.Global;
import com.weichai.common.mapper.JsonMapper;
import com.weichai.common.utils.IdGen;
import com.weichai.common.utils.StringUtils;
import com.weichai.modules.act.service.ActTaskService;
import com.weichai.modules.sys.entity.User;
import com.weichai.modules.sys.utils.DictUtils;
import com.weichai.modules.sys.utils.UserUtils;
import com.weichai.modules.task.dwrUtil.ProtocolDwrUtils;
import com.weichai.modules.task.entity.BenchResourceInfo;
import com.weichai.modules.task.service.BenchResourceInfoService;
import com.weichai.modules.webservice.entity.JsonResult;
import com.weichai.modules.webservice.service.AnalysisResultService;
import com.weichai.modules.webservice.service.BenchOrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import sun.misc.BASE64Decoder;

import javax.jws.WebService;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * 台架预定webservice
 * @author lxm
 * @version 2017-09-13
 */
@WebService(endpointInterface = "com.weichai.modules.webservice.service.AnalysisResultService", serviceName = "analysisResultService",
        targetNamespace="http://service.webservice.modules.weichai.com/")
@Component
public class AnalysisResultServiceImpl implements AnalysisResultService {

    /**
     * 日志对象
     */
    protected Logger logger = LoggerFactory.getLogger(getClass());
    private String baseUrl = Global.getConfig("dir");
    /**
     * 实验结果webservice接口
     * @param  id
     * @param imgSuffix:图片扩展名
     * @param imgStr :base64编码
     * @return
     */
    public String getTaskResultInfo(String id,String imgSuffix , String imgStr){
        logger.info("调用实验结果webservice接口-------------------start");
        if (null==imgStr){
            return JsonMapper.toJsonString(new JsonResult(false,"图片为空!"));
        }
        if(null==id){
            return JsonMapper.toJsonString(new JsonResult(false,"id为空!"));
        }
        String picurl = DictUtils.getDictLabel("06", "file_url", "");
        String picName= String.valueOf(System.currentTimeMillis());
        //实验结果图片存放路径
        String path = baseUrl+ File.separator+picurl;
        File file = new File(path);
        if (!file.exists()) {
            file.mkdir();
        }
        String picPath = path+File.separator+picName+"."+imgSuffix;
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            byte[] b = decoder.decodeBuffer(imgStr);
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {
                    b[i] += 256;
                }
            }
            OutputStream out = new FileOutputStream(picPath);
            out.write(b);
            out.flush();
            out.close();
        } catch (Exception e) {
                return JsonMapper.toJsonString(new JsonResult(false,e.getMessage()));
        }
        logger.info("调用实验结果webservice接口-------------------end");
        return JsonMapper.toJsonString(new JsonResult(true,"操做成功!"));
    }

}

3.spring_context.xml中配置spring

<!-- webService 接口 -->
<import resource="classpath:META-INF/cxf/cxf.xml"/>      //cxf jar包
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>  //cxf jar包

<jaxws:endpoint id="AnalysisResultService"
            implementor="com.weichai.modules.webservice.service.impl.AnalysisResultServiceImpl" address="/analysisResultService" >
</jaxws:endpoint>

4.web.xml中配置apache

<servlet>
   <description>CXF Endpoint</description>
   <display-name>cxf</display-name>
   <servlet-name>cxf</servlet-name>
   <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
   <load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
   <servlet-name>cxf</servlet-name>
   <url-pattern>/ws/*</url-pattern>
</servlet-mapping>

5.启动项目后,在浏览器上调用,看看是否生成的wsdl文件正确浏览器

http://localhost:8080/ws/analysisResultService?wsdlapp

备注:curl

@WebService(endpointInterface= "com.weichai.modules.webservice.service.AnalysisResultService", serviceName="analysisResultService",targetNamespace="http://service.webservice.modules.weichai.com/")编码

定义说明书的显示方法
1.url

@WebService(serviceName="PojoService", portName="PojoPort", name="PojoPortType", targetNamespace="http//:Pojo")
  serviceName 对应 <service name="PojoService">
  portName 对应 <service>下的 <port name="PojoPort">          
      name 对应 <portType name="PojoPortType">spa

     targetNamespace 对应 targetNamespace="http//:Pojo"

2.

    一、serviceName: 对外发布的服务名,指定 Web Service 的服务名称:wsdl:service。缺省值为 Java 类的简单名称 + Service。(字符串)

    二、endpointInterface: 服务接口全路径, 指定作SEI(Service EndPoint Interface)服务端点接口

    三、name:此属性的值包含XML Web Service的名称。在默认状况下,该值是实现XML Web Service的类的名称,wsdl:portType 的名称。缺省值为 Java 类的简单名称 + Service。(字符串)

    四、portName:  wsdl:portName。缺省值为 WebService.name+Port。

    五、targetNamespace:指定你想要的名称空间,认是使用接口实现类的包名的反缀

    六、wsdlLocation:指定用于定义 Web Service 的 WSDL 文档的 Web 地址。Web 地址能够是相对路径或绝对路径。(字符串)

    注意:实现类上能够不添加Webservice注解  

相关文章
相关标签/搜索