解决cxf+spring发布的webservice缺少<wsdl:types>和<wsdl:message>标签的问题

最近学习cxf+spring发布webservice服务,但是遇到服务发布成功,查看其wsdl文件的时候却缺少<wsdl:import><wsdl:message>标签,查看代码确实没什么问题,因为用来测试,代码比较简单,但就是不出现以上两个标签。百度谷歌到处找原因找不到。失望……

还是自己好好动手解决问题吧。

认真看发布后的wsdl文件,发现多了<wsdl:import>标签(<wsdl:importlocation="http://localhost:8080/cxf-Spring-server/Hello?wsdl=HelloWorld.wsdl" namespace="http://dao.testCxfSpring.spg.com/"></wsdl:import>),将<wsdl:import>标签中的location拿出去在浏览器中打开,里面是“丢失”的两个标签,这时候就发现其实并不是丢失了,而是包含在了<wsdl:import>标签内

为什么会包含在了<wsdl:import>标签内?再研究,发现<wsdl:definitions>标签内的targetNamespace属性和<wsdl:import>namespace属性的值不同,发现原因所在

最终解决方案

1,将接口类和实现类放在同一个包下,问题即可解决

2,将接口类和实现类中的注解中加入命名空间属性配置,@WebService(targetNamespace="XXXXX"),两个配置成一样


http://blog.csdn.net/fengspg/article/details/7404383

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一个简单的示例。 1. 创建一个新的Maven项目,添加如下依赖: ```xml <!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>5.1.9.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.1.9.RELEASE</version> </dependency> <!-- CXF --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-core</artifactId> <version>3.3.4</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>3.3.4</version> </dependency> <!-- JSP --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> <scope>provided</scope> </dependency> <!-- Other dependencies --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> ``` 2. 创建一个新的Spring配置文件 applicationContext.xml,添加如下内容: ```xml <!-- 配置CXF --> <bean id="cxf" class="org.apache.cxf.bus.spring.SpringBus"> <property name="cxfConfigLocation" value="classpath:META-INF/cxf/cxf.xml"/> </bean> <!-- 配置WebService --> <jaxws:endpoint id="userService" implementor="com.example.service.impl.UserServiceImpl" address="/UserService"/> <!-- 配置Spring MVC --> <context:component-scan base-package="com.example.controller"/> <mvc:annotation-driven/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> </bean> ``` 3. 创建一个新的CXF配置文件 cxf.xml,添加如下内容: ```xml <beans xmlns="http://www.springframework.org/schema/beans" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:cxf="http://cxf.apache.org/core" xmlns:tns="http://example.com/service"> <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/> <import resource="classpath:META-INF/cxf/cxf-extension-xml.xml"/> <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/> <cxf:bus> <cxf:features> <wsdl:logging/> </cxf:features> </cxf:bus> <jaxws:endpoint xmlns:tns="http://example.com/service" id="userService" implementor="com.example.service.impl.UserServiceImpl" address="/UserService"> <jaxws:serviceFactory> <ref bean="cxf"/> </jaxws:serviceFactory> </jaxws:endpoint> </beans> ``` 4. 创建一个新的Java类 UserService,添加如下内容: ```java package com.example.service; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebService; @WebService public interface UserService { @WebMethod public boolean login(@WebParam(name = "username") String username, @WebParam(name = "password") String password); } ``` 5. 创建一个新的Java类 UserServiceImpl,添加如下内容: ```java package com.example.service.impl; import javax.jws.WebService; import com.example.service.UserService; @WebService(endpointInterface = "com.example.service.UserService") public class UserServiceImpl implements UserService { @Override public boolean login(String username, String password) { // Your login logic here return true; } } ``` 6. 创建一个新的Spring MVC控制器 UserController,添加如下内容: ```java package com.example.controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.xml.namespace.QName; import javax.xml.ws.Service; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import com.example.service.UserService; @Controller public class UserController { private static final String SERVICE_URL = "http://localhost:8080/demo/UserService?wsdl"; private static final QName SERVICE_NAME = new QName("http://example.com/service", "UserService"); @RequestMapping("/login") public ModelAndView login(HttpServletRequest request, HttpServletResponse response) throws Exception { String username = request.getParameter("username"); String password = request.getParameter("password"); // Create a new Service instance Service service = Service.create(SERVICE_URL, SERVICE_NAME); // Get the UserService instance from the Service UserService userService = service.getPort(UserService.class); // Call the login method boolean result = userService.login(username, password); // Return the result ModelAndView mav = new ModelAndView(); if (result) { mav.setViewName("success"); } else { mav.setViewName("error"); } return mav; } } ``` 7. 创建一个新的JSP视图 success.jsp,添加如下内容: ```jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Login Success</title> </head> <body> <h1>Login Success</h1> </body> </html> ``` 8. 创建一个新的JSP视图 error.jsp,添加如下内容: ```jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Login Error</title> </head> <body> <h1>Login Error</h1> </body> </html> ``` 9. 在Web.xml中添加如下内容: ```xml <servlet> <servlet-name>cxf</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>cxf</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> ``` 10. 运行项目,并在浏览器中访问 http://localhost:8080/demo/login,输入用户名和密码,点击登录按钮,可以看到登录成功或失败的页面。 希望这个示例能对您有所帮助。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值