ws问题总结(二)

来源:互联网 发布:清除软件残留 编辑:程序博客网 时间:2024/05/16 05:47

1、a  > 建一个web项目,添加完spring+hibernate的jar包以后,添加jax-ws 2.1等jar路径;

      b  >  然后写一个java类,带上@WebService和@WebMethod,

      c  >  使用Myeclipse自动生成WS,会自动生成wsdl、web.xml中的配置等,applicationContext.xml中不用额外配置

      (加上@,则不会生成TgsFuncitonDelegate)

      d  >   在spring中,toTell中注入private WSAction wsaction,wsaction使用时报空指针,实例化toTell时wsaction没有注入成功,需要这样得到wsaction

 

    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    WSAction wsaction= (TgsPassedCarAction)context.getBean("WSAction");

e  >  (jar包:spring  + 

                       hibernate  + 

                       jax-ws 2.1 RunTime Liberaries  + 

                       jax-ws 2.1 API Liberaries)

----------------------------------------------------------web.xml中自动生成的部分---------------------------------------------------------------------------------------------------------

-------------------------------------还有记得是WSServlet,不要和后边的WSSpringServlet混了----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

<?xml version="1.0" encoding="UTF-8"?><web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">  <display-name></display-name> <span style="color:#ff0000;"> <listener>  <listener-class>  com.sun.xml.ws.transport.http.servlet.WSServletContextListener  </listener-class>  </listener></span>  <servlet>  <description>JAX-WS endpoint - toDoService</description>  <display-name>toDoService</display-name>  <servlet-name>toDoService</servlet-name>  <servlet-class>  <span style="color:#ff0000;">com.sun.xml.ws.transport.http.servlet.WSServlet</span>  </servlet-class>  <load-on-startup>1</load-on-startup>  </servlet>  <servlet-mapping>  <servlet-name>toDoService</servlet-name>  <url-pattern>/toDoPort</url-pattern>  </servlet-mapping></web-app>


 

 

 

 

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

2、通过spring注入的方式,而不用再context.getBean()的方式获得注入的bean组件,更加方便

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

          a  >  首先加上Spring+jax-ws整合所需要的jar包,见下面,spring和hibernate的jar包随意加;

          b  >  写一个java类,要加上@WebService(targetNamespace = "org.coderecord.blog"),括号里的没加的话,我这里报错(Caused by: com.sun.xml.ws.model.RuntimeModelerException: 必须在没有程序包的类上指定 @WebService.targetNamespace。类: Test);

                 @WebMethod(exclude = true)使get/set方法对外不可见;

<p>import javax.jws.WebMethod;import javax.jws.WebService;</p><p>@WebService(targetNamespace = "org.coderecord.blog")public class Test { private TestBean testBean; @WebMethod public String toTest(String name){  String daming = testBean.tosat("lvlvlv");  System.out.println(name+": "+daming);  return name+":"+daming; } @WebMethod(exclude = true) public TestBean getTestBean() {  return testBean; } @WebMethod(exclude = true) public void setTestBean(TestBean testBean) {  this.testBean = testBean; }}</p>

              c  >   接着,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" <span style="color:#ff0000;">xmlns:ws="http://jax-ws.dev.java.net/spring/core"</span>     <span style="color:#ff0000;"> xmlns:wss="http://jax-ws.dev.java.net/spring/servlet"</span> xmlns:p="http://www.springframework.org/schema/p"      xmlns:context="http://www.springframework.org/schema/context"      xsi:schemaLocation="http://www.springframework.org/schema/beans      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd     <span style="color:#ff0000;"> http://jax-ws.dev.java.net/spring/core     http://jax-ws.dev.java.net/spring/core.xsd     http://jax-ws.dev.java.net/spring/servlet     http://jax-ws.dev.java.net/spring/servlet.xsd"></span>          <bean id="test" class="Test">     <property name="testBean">     <ref bean="testBean"/>     </property>     </bean>     <bean id="testBean" class="TestBean"></bean>     <span style="color:#ff0000;"> <wss:binding url="/service/test">         <wss:service>             <ws:service bean="#test" />         </wss:service>     </wss:binding></span></beans>


            d  >  web.xml中手动配置,注意下面红色的代码和第一种jax-ws写法中的WSServlet 不是一个Servlet

<?xml version="1.0" encoding="UTF-8"?><web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">  <display-name></display-name>   <context-param>    <param-name>contextConfigLocation</param-name>    <param-value>/WEB-INF/classes/*Context.xml</param-value>  </context-param>  <listener>    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  </listener>  <servlet>    <description>JAX-WS endpoint - test</description>    <display-name>testService</display-name>    <servlet-name>testService</servlet-name>    <span style="color:#ff0000;"><servlet-class>com.sun.xml.ws.transport.http.servlet.WSSpringServlet</servlet-class></span>    <load-on-startup>1</load-on-startup>  </servlet>  <servlet-mapping>    <servlet-name>testService</servlet-name>    <url-pattern>/service/test</url-pattern>  </servlet-mapping>  <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list></web-app>



 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

0 0
原创粉丝点击