书写webservice接口

1.书写Service       webservice注解

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注解

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中配置

<!-- 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中配置

<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?wsdl

备注:

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

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

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

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

2.

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

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

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

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

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

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

    注意:实现类上可以不添加Webservice注解  

转载于:https://my.oschina.net/u/3409039/blog/1572943

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值