webService之helloword(web)

Posted fjk

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了webService之helloword(web)相关的知识,希望对你有一定的参考价值。

spring 整合webservice

pom.xml文件

	<dependencies>
		<!-- CXF WS开发 -->
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-frontend-jaxws</artifactId>
			<version>3.0.1</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>4.1.7.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>4.1.7.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>4.1.7.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
		</dependency>

	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>tomcat-maven-plugin</artifactId>
				<version>1.1</version>
				<configuration>
					<port>9998</port>
				</configuration>
			</plugin>
		
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
	</build>

  web.xml文件

 <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <servlet>
    <servlet-name>CXFService</servlet-name>
    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>CXFService</servlet-name>
    <url-pattern>/services/*</url-pattern>
  </servlet-mapping>

  applicationContext.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:jaxws="http://cxf.apache.org/jaxws"
	xsi:schemaLocation="
	http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
	http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

	<!-- 
		
		serviceClass 服务接口
		address 服务访问地址 
	 -->
	<jaxws:server id="userService" address="/userService" 
		serviceClass="com.baidu.service.UserService">
		<jaxws:serviceBean>
			<bean class="com.baidu.service.imp.UserServiceImp" />
		</jaxws:serviceBean>
	</jaxws:server>
	
</beans>

  

包结构

  UserService接口

package com.baidu.service;

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

import com.baidu.domain.User;

@WebService
public interface UserService {
	@WebMethod
	public User get(Integer id);
	@WebMethod
	public void eat();
}

  UserServiceImp实现类

package com.baidu.service.imp;

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

import com.baidu.domain.User;
import com.baidu.service.UserService;
@WebService
public class UserServiceImp implements UserService {

	public User get(Integer id) {
		if(id==1){
			User u=new User();
			u.setId(2);
			u.setName("张三");
			return u;
		}
		return null;
	}

	@Override
	public void eat() {
		System.out.println("123");
		
	}

}

  新建一个maven web project项目

pom.xml文件不变

applicationContext.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:jaxws="http://cxf.apache.org/jaxws"
	xsi:schemaLocation="
	http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
	http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

	<!-- 
		address 客户端访问服务路径 
		serviceClass 配置接口
		serviceBean 配置实现类 -->
	<jaxws:client id="userServiceClient" 
		serviceClass="com.baidu.service.UserService" 
		address="http://localhost:9998/werservicespring/services/userService" >
		<!-- 来源消息拦截器
		<jaxws:inInterceptors>
			<bean class="org.apache.cxf.interceptor.LoggingInInterceptor"/>
		</jaxws:inInterceptors> -->
		<!-- 输出消息拦截器 
		<jaxws:outInterceptors>
			<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor" />
		</jaxws:outInterceptors>-->
	</jaxws:client>
	
	
	
</beans>  

在测试之前需要创建接口  包结构也需要一样

package com.baidu.service;

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

import com.baidu.domain.User;

@WebService
public interface UserService {
	@WebMethod
	public User get(Integer id);
	@WebMethod
	public void eat();
}

  

测试类

package com.baidu.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.baidu.domain.User;
import com.baidu.service.UserService;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext.xml")
public class Test01 {
	@Autowired
	private UserService userService;
	@Test
	public void test01(){
		//userService.eat();
		User user = userService.get(1);
		System.out.println(user);
	}
}

  

 

以上是关于webService之helloword(web)的主要内容,如果未能解决你的问题,请参考以下文章

webService之helloword(java)rs

WebService

webservice创建部署和调用

框架学习Struts2之HelloWord

WebService 之 SoapHeader

WebService_Demo