zhouhaifang8706 2010-07-30 15:59
浏览 210
已采纳

这个程序的实现原理问题,请您回答的越详细越好!万分感谢

(2)配置文件说明

applicationContext.xml文件的内容如下:

view plaincopy to clipboardprint?
<?xml version="1.0" encoding="UTF-8"?>

xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">









services.xml文件的内容如下:

view plaincopy to clipboardprint?
<?xml version="1.0" encoding="UTF-8"?>

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="/HelloWorld" implementor="com.cxf.test.interfaces.HelloWorldImpl"/>

web.xml文件的内容如下:

view plaincopy to clipboardprint?
<?xml version="1.0" encoding="UTF-8"?>



cxfservice



contextConfigLocation

WEB-INF/classes/applicationContext.xml







org.springframework.web.context.ContextLoaderListener







CXFServlet



org.apache.cxf.transport.servlet.CXFServlet







CXFServlet

/services/*

3)发布的HelloWord服务说明

要发布的HelloWorld服务的接口定义文件com.cxf.test.interfaces.HelloWorld:

package com.cxf.test.interfaces;

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

@WebService
public interface HelloWorld
{
/*

  • 一个简单的方法,返回一个字符串
  • @param hello
  • @return
    */
    String say(String hello);

    /**

  • 稍微复杂一些的方法,传递一个对象给服务端处理

  • @param user

  • @return
    */
    String sayUserName(@WebParam(name = "user") UserDTO user);

    /**

  • 最复杂的方法,返回一个List封装的对象集合

  • @return
    */
    public @WebResult(partName = "o") ListObject findUsers();
    }

    要发布的HelloWorld服务的接口实现类com.cxf.test.interfaces.HelloWorldImpl:

view plaincopy to clipboardprint?
package com.cxf.test.interfaces;

import java.util.ArrayList;

import javax.jws.WebService;

/**

  • @author zhangzk
  • /
    /
    *
  • WebService实现类.

  • 使用@WebService指向Interface定义类即可.

    */

    @WebService(endpointInterface = "com.cxf.test.interfaces.HelloWorld")

    public class HelloWorldImpl implements HelloWorld

    {

    public String sayUserName(UserDTO user)

    {

    return "hello " + user.getName();

    }

    public String say(String hello)

    {

    return "hello " + hello;

    }

    public ListObject findUsers()

    {

    ArrayList list = new ArrayList();

    list.add(instancUser(1, "lib"));   
    list.add(instancUser(2, "mld"));   
    list.add(instancUser(3, "lq"));   
    list.add(instancUser(4, "gj"));   
    ListObject o = new ListObject();   
    o.setList(list);   
    return o;   
    

    }

    private UserDTO instancUser(Integer id, String name)

    {

    UserDTO user = new UserDTO();

    user.setId(id);

    user.setName(name);

    return user;

    }

    }

    findUsers()接口返回的参数对象定义文件com.cxf.test.interfaces.ListObject:

view plaincopy to clipboardprint?
package com.cxf.test.interfaces;

import java.util.ArrayList;

import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;

import javax.xml.bind.annotation.XmlAccessorType;

import javax.xml.bind.annotation.XmlElement;

import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)

@XmlType(name = "listObject", propOrder ={ "list" })

public class ListObject

{

@XmlElement(nillable = true)

protected List list;

/**  
 * Gets the value of the list property.  
 *   
 * <p>  
 * This accessor method returns a reference to the live list, not a snapshot. Therefore any modification you make to the returned list will be  
 * present inside the JAXB object. This is why there is not a <CODE>set</CODE> method for the list property.  
 *   
 * <p>  
 * For example, to add a new item, do as follows:  
 *   
 * <pre>  
 * getList().add(newItem);  
 * </pre>  
 *   
 *   
 * <p>  
 * Objects of the following type(s) are allowed in the list {@link Object }  
 *   
 *   
 */  
public List<Object> getList()   
{   
    if (list == null)   
    {   
        list = new ArrayList<Object>();   
    }   
    return this.list;   
}   

public void setList(ArrayList<Object> list)   
{   
    this.list = list;   
}   

}

UserDTO instancUser(Integer id, String name)接口返回的对象定义文件com.cxf.test.interfaces.UserDTO:

view plaincopy to clipboardprint?
package com.cxf.test.interfaces;

import javax.xml.bind.annotation.XmlAccessType;

import javax.xml.bind.annotation.XmlAccessorType;

import javax.xml.bind.annotation.XmlType;

/**

  • Web Service传输User信息的DTO.

  • 分离entity类与web service接口间的耦合,隔绝entity类的修改对接口的影响. 使用JAXB 2.0的annotation标注JAVA-XML映射,尽量使用默认约定.
  • */

    @XmlAccessorType(XmlAccessType.FIELD)

    @XmlType(name = "User")

    public class UserDTO

    {

    protected Integer id;

    protected String name;

    public Integer getId()

    {

    return id;

    }

    public void setId(Integer value)

    {

    id = value;

    }

    public String getName()

    {

    return name;

    }

    public void setName(String value)

    {

    name = value;

    }

    }

http://127.0.0.1:8080/cfx_spring_webservice/services/HelloWorld?wsdl 得出的结果为

<?xml version="1.0" encoding="UTF-8" ?>

  • 写回答

3条回答 默认 最新

  • layer555 2010-07-30 17:12
    关注

    实现原理? 这个不就是使用CXF来发布Web service吗?CXF本身提供了一些可以与Spring结合的东西用来分析Service上的哪些注解,然后注解设置发布Web service。web.xml中的CXFServlet则是用来处理Web service请求的。简单来说就是这样了,有兴趣的话可以研究下CXF的配置,想更深入了解的话可以研究下CXF的源码,很不错的东西。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?