Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

集成Apache CXF并对外提供WebService接口示例 #219

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,13 @@
<artifactId>UserAgentUtils</artifactId>
<version>1.13</version>
</dependency>

<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>apache-cxf</artifactId>
<version>3.1.4</version>
<type>zip</type>
</dependency>

<!-- 自定义jar依赖包
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.thinkgem.jeesite.ws.test;

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

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

import com.thinkgem.jeesite.modules.sys.entity.User;

@WebService
public interface HelloWorldWebService {

// 注意接口需要定义@WebParam注解,否则WSDL中的接口请求参数名称会变成arg0, arg1...
String sayHi(@WebParam(name = "text") String text);

List<WsResponseUser> userList();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.thinkgem.jeesite.ws.test;

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

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

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.thinkgem.jeesite.common.utils.SpringContextHolder;
import com.thinkgem.jeesite.modules.sys.dao.UserDao;
import com.thinkgem.jeesite.modules.sys.entity.User;

/***
* 服务发布后可通过 http://localhost:8080/jeesite/webservice/HelloWorld?wsdl访问
*
* */

@WebService()
public class HelloWorldWebServiceImpl implements HelloWorldWebService {

public String sayHi(String text) {
System.out.println("sayHi called");
return "Hello " + text;
}

@Override
public List<WsResponseUser> userList() {
UserDao userDao = SpringContextHolder.getBean(UserDao.class);
User query = new User();
query.getPage().setPageNo(1);
query.getPage().setPageSize(100);
List<User> users = userDao.findList(query);
List<WsResponseUser> ret = Lists.newArrayList();
for (User user : users) {
WsResponseUser resp = new WsResponseUser(user.getName(),
user.getNo(), user.getLoginName());
ret.add(resp);
}
return ret;
}

}
39 changes: 39 additions & 0 deletions src/main/java/com/thinkgem/jeesite/ws/test/WsResponseUser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.thinkgem.jeesite.ws.test;

public class WsResponseUser {
private String name;

private String username;

private String workid;

public WsResponseUser(String name, String workid, String username) {
this.name = name;
this.workid = workid;
this.username = username;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getWorkid() {
return workid;
}

public void setWorkid(String workid) {
this.workid = workid;
}
}
17 changes: 17 additions & 0 deletions src/main/resources/spring-context-cxf.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?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:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">

<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

<!-- 测试用 -->
<bean id="hello" class="com.thinkgem.jeesite.ws.test.HelloWorldWebServiceImpl" />
<jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" />

</beans>
11 changes: 11 additions & 0 deletions src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,17 @@
<url-pattern>/servlet/validateCodeServlet</url-pattern>
</servlet-mapping>

<!-- Apache CXF WebService -->
<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>/webservice/*</url-pattern>
</servlet-mapping>

<!-- FineReport
<servlet>
<servlet-name>ReportServer</servlet-name>
Expand Down