版权声明:内容记录学习过成文章,仅供参考 https://blog.csdn.net/qq_40195958/article/details/83502736
apache的axis需要的jar
<!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-frontend-jaxws -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-transports-http -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.4</version>
</dependency>
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis-jaxrpc</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>axis</groupId>
<artifactId>axis-wsdl4j</artifactId>
<version>1.5.1</version>
</dependency>
接收发布的WebServcie
public String getRunNameTree() throws ServiceException, RemoteException {
WebUtil util = new WebUtil();
String addess = util.getDepartmentAddress();
String uri = util.getDepartmentURI();
String getStaffList = util.getGetStaffList();
Service service = new Service();
Call call = (Call) service.createCall();
// webservice 访问地址
call.setTargetEndpointAddress(addess);
call.setSOAPActionURI("");
call.setUseSOAPAction(true);
// webservice 命名空间和方法名
call.setOperationName(new QName(uri,getStaffList));
call.setReturnType(XMLType.XSD_STRING);
// webservice参数的名称
call.addParameter("param", org.apache.axis.encoding.XMLType.XSD_STRING,
javax.xml.rpc.ParameterMode.IN);
JSONObject json = new JSONObject();
json.put("depId", "2c9080f2491297ea0149129956bf0005");
// 调用webservice 获取返回值,json.toString为传递参数
String result = (String) call.invoke(new Object[] { json.toString()});
return result;
}
spring配置发布webService
配置文件
<!-- 配置扫描实现类路径,设置地址 -->
<jaxws:endpoint id="standard" implementor="com.jit.webService.app.webservice.impl.StandardWebServiceImpl" address="/standard"/>
web.xml文件中增加拦截配置
<!-- cxf webservices -->
<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>/services/*</url-pattern>
</servlet-mapping>
写service接口
service层
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService(targetNamespace = "http://webservice.app.ota4.jit.com")
public interface StandardWebService {
@WebMethod
public String getProjectInfoIdByDepId(@WebParam(name = "param") String param)
throws Exception;
}
实现类、最终返回数据封装成json进行交互
import javax.jws.WebParam;
import javax.jws.WebService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jit.ota4.app.service.thirddev.ThirdDevService;
import com.jit.ota4.app.webservice.StandardWebService;
@WebService(endpointInterface = "com.jit.webService.app.webservice.StandardWebService", serviceName = "StandardWebService")
public class StandardWebServiceImpl implements StandardWebService {
@Autowired
private ThirdDevService thirdDevService;
@Override
public String getProjectInfoIdByDepId(@WebParam(name = "param") String param)
throws Exception {
ResultReturn rr = new ResultReturn();
if (StringUtils.isNotBlank(param) && StringUtils.isNotEmpty(param)) {
try {
String result = thirdDevService.getProjectInfoIdByDepId(param);
JSONObject obj = JSON.parseObject(result);
if (StringUtils.isNotBlank(result)
&& StringUtils.isNotEmpty(result)) {
rr.setCount(obj.getString("count"));
rr.setParam(obj.getString("result"));
rr.setIssuccess("0");
} else {
rr.setCount("0");
rr.setParam("");
rr.setIssuccess("0");
}
} catch (Exception e) {
e.printStackTrace();
rr.setIssuccess("1");
}
return JSON.toJSONString(rr, true);
}
return null;
}
}
发布配置访问接口地址
http://localhost/webService/services/standard/StandardWebService?wsdl