cxf发布webservce的2种方式

cxf发布webservce的2种方式

一.使用配置文件
1.配置web.xml

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath*:/spring-context*.xml</param-value>
	</context-param>
	<!-- cxf servlet -->
	<servlet>
		<servlet-name>CXFServlet</servlet-name>
		<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
	</servlet>

	<servlet-mapping>
		<servlet-name>CXFServlet</servlet-name>
		<url-pattern>/ws/*</url-pattern>
	</servlet-mapping>

2.配置spring-context-webservice.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jaxws="http://cxf.apache.org/jaxws"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://cxf.apache.org/jaxws 
        http://cxf.apache.org/schemas/jaxws.xsd">
        
	<import resource="classpath:META-INF/cxf/cxf.xml" />
	<jaxws:endpoint implementor="com.lmp.webservice.impl.DataWebServiceImpl" address="/data">
	</jaxws:endpoint>
</beans>

3.接口类

package com.lmp.webservice;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

@WebService
public interface DataWebService {
	
	/**
	 * 获取用户信息
	 * @param userId 用户编号
	 * @return JSON字符串
	 */
	@WebMethod
	String getUser(@WebParam(name="userId")String userId);
}

4.实现类

package com.lmp.webservice.impl;

import java.util.HashMap;
import java.util.Map;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.druid.support.json.JSONUtils;
import com.lmp.service.UserService;
import com.lmp.webservice.DataWebService;

@WebService(endpointInterface="com.lmp.webservice.DataWebService")
public class DataWebServiceImpl implements DataWebService {
	
	
	@Autowired
	private UserService userService;
	
	/**
	 * 获取用户信息
	 * @param userId 用户编号
	 * @return JSON字符串
	 */
	@Override
	public String getUser(String userId) {
		User user= userService.getUserByUserId(userId);
		Map<String, String> map = new HashMap<>();
		map.put("name", user.getUserName());
		map.put("sex", user.getUserSex());
		return JSONUtils.toJSONString(map);
	}
}

5.发布路径:http://localhost:8080/PP/ws/data?wsdl
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

如下则发布成功:
在这里插入图片描述
二.使用spring注入
1.其他同方法一,仅仅不使用spring-context-webservice.xml
WebServiceConfig.java

package com.lmp.common.config;


import com.lmp.webservice.DataWebService;
import org.apache.cxf.jaxws.EndpointImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.xml.ws.Endpoint;

@Configuration
public class WebServiceConfig {

    @Autowired
    private DataWebService dataWebServce;

    /**
     * 发布服务webservice
     * @return
     */
    @Bean
    public Endpoint createEndpoint(){
        return EndpointImpl.publish("/data", dataWebServce);
    }
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值