webservice的釋出(服務端)
1.web,xml配置釋出監聽器
2.監聽類的編寫
3.介面的編寫
*** 該webservice釋出會後 http://localhost:8089/user 連結即可訪問
http://localhost:8089/user?wsdl 也可訪問 並返回xml格式
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name>Archetype Created Web Application</display-name>
<!-- 掃描載入配置檔案 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring/applicationContext.xml,
classpath:spring/applicationContext-dataSource.xml
</param-value>
</context-param>
<!--log4j配置檔案開始-->
<context-param>
<param-name>log4jConfigLocation</param-name>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<!-- webservice釋出的監聽器 -->
<listener>
<listener-class>com.fxb.listener.MyListener</listener-class>
</listener>
<!-- 統計線上人數 監聽器 -->
<listener>
<listener-class>com.fxb.listener.MyListener2</listener-class>
</listener>
<!-- post方式提交 統一字元編碼過濾器 -->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- springmvc攔截所有的請求交由各個處理器 -->
<servlet>
<servlet-name>springMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
監聽類MyListener.java
package com.fxb.listener;
import javax.servlet.ServletContextEvent;
import javax.xml.ws.Endpoint;
import org.springframework.web.context.ContextLoaderListener;
import com.fxb.service.impl.UserServiceImpl;
public class MyListener extends ContextLoaderListener {
public void contextInitialized(ServletContextEvent event) {
String address="http://localhost:8008/user"; //該地址可以是任意,埠號自己確定 釋出後該連結可以訪問
Endpoint.publish(address,new UserServiceImpl()); //是將服務釋出到本地的jdk自帶的web容器中
super.contextInitialized(event); //contextInitialized也就是監聽器類的main入口函式
}
}
UserServiceImpl.java
package com.fxb.service;
import org.springframework.stereotype.Service;
public interface UserService {
void save () throws Exception;
}