github代码:https://github.com/benjiaxiaoxu/jscxfweb

最近一直在业余的时间想搞一个类似糗事百科的服务端做为客户端的信息来源,查阅资料绝对使用apache的cxf来作为webservice服务

翻阅了大量资料发现一些代码,但没有完善的代码体系,我提供一个吧,这个是我写的服务端,里面还有一部分需要完善,大家看到问题后可以给我留言!

不多说了,上部分代码,完整代码参考上面我的github

applicationContext-server.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jaxws="http://cxf.apache.org/jaxws"
    xmlns:jaxrs="http://cxf.apache.org/jaxrs"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    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.0.xsd
    http://cxf.apache.org/jaxws 
    http://cxf.apache.org/schemas/jaxws.xsd
    http://cxf.apache.org/jaxrs
    http://cxf.apache.org/schemas/jaxrs.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    
<import resource="classpath:META-INF/cxf/cxf.xml"/>
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>

 
    <bean id="restUser" class="com.benjiaren.service.impl.UserServiceImpl"/>
    <bean id="restJiongshi" class="com.benjiaren.service.impl.JiongShiServiceImpl"/>
    <bean id="restComment" class="com.benjiaren.service.impl.CommentServiceImpl"/>
    
        
    
    <jaxrs:server id="restServiceContainer" address="/service">
        <jaxrs:serviceBeans>
            <ref bean="restUser" />
            <ref bean="restJiongshi" />
            <ref bean="restComment" />
        </jaxrs:serviceBeans>
        <jaxrs:extensionMappings>
            <entry key="json" value="application/json" />
            <entry key="xml" value="application/xml" />
        </jaxrs:extensionMappings>
        <jaxrs:languageMappings>
               <entry key="en" value="en-gb"/>  
        </jaxrs:languageMappings>
    </jaxrs:server>
    
    
    
    
</beans>

提供一个参考类:

package com.benjiaren.service;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
//import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;

import com.benjiaren.bean.JiongShiList;

/**
 * 囧事服务类
 * @author xuzhiguang
 *
 */

@Path(value = "/jiongshi")   
//@Produces("application/json") 
public interface JiongShiService {
    
    /**
     * 根据用户名查询
     * @param username
     * @return 所有囧事
     */
    
    @GET  
    @Path(value = "/{username}/search")  
    public JiongShiList findUserByName(@PathParam("username")String username);
    
    /**
     * 通过get方式发布囧事
     * @param text
     * @param source
     * @param username
     * @param longitude
     * @param latitude
     */
    @GET  
    @Path(value = "/add")  
    public void addJiongShiget(@QueryParam("text")String text,@QueryParam("source")String source,
            @QueryParam("username")String username,@QueryParam("longitude")String longitude,@QueryParam("latitude")String latitude);
    
    /**
     * 通过post方式发布囧事
     * @param text
     * @param source
     * @param username
     * @param longitude
     * @param latitude
     */
    @POST 
    @Path(value = "/add")  
    public void addJiongShipost(@QueryParam("text")String text,@QueryParam("source")String source,
            @QueryParam("username")String username,@QueryParam("longitude")String longitude,@QueryParam("latitude")String latitude);
    
    /**
     * 得到所有的囧事
     * @return 所有囧事
     */
    @GET  
    @Path(value = "/search")  
    public JiongShiList findALLJiongShi();
    
    /**
     * 得到最近的囧事
     * @return 所有囧事
     */
    @GET  
    @Path(value = "/searchByCount")  
    public JiongShiList findALLJiongShiByCount(@QueryParam("count")int count);
    
    /**
     * 得到从start到stop的囧事
     * @return 所有囧事
     */
    @GET  
    @Path(value = "/searchByStartStop")  
    public JiongShiList findALLJiongShiByStartStop(@QueryParam("start")int start,@QueryParam("stop")int stop);
    
    /**
     * 
     * @param id
     * @param username
     */
    @GET  
    @Path(value = "/addRepostsCountG")  
    public void addRepostsCountGet(@QueryParam("id")int id,@QueryParam("username")String username);
    
    /**
     * 
     * @param id
     * @param username
     */
    @POST 
    @Path(value = "/addRepostsCountP")  
    public void addRepostsCountPost(@QueryParam("id")int id,@QueryParam("username")String username);
    /**
     * 
     * @param id
     * @param username
     */
    @GET  
    @Path(value = "/addAttitudesCounG")  
    public void addAttitudesCountGet(@QueryParam("id")int id,@QueryParam("username")String username);
    
    /**
     * 
     * @param id
     * @param username
     */
    @POST 
    @Path(value = "/addAttitudesCounP")  
    public void addAttitudesCountPost(@QueryParam("id")int id,@QueryParam("username")String username);
    
}

jiongshilist

package com.benjiaren.bean;

import java.util.List;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "jsList")
public class JiongShiList {
    private List<JiongShi> jiongshi;

    public List<JiongShi> getJiongshi() {
        return jiongshi;
    }

    public void setJiongshi(List<JiongShi> jiongshi) {
        this.jiongshi = jiongshi;
    }

    
    
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
    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_2_5.xsd">
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
  <!-- 加载Spring容器配置 -->
      <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
  <!-- 设置Spring容器加载配置文件路径 -->
      <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:applicationContext-server.xml</param-value>
      </context-param>
 
      <listener>
        <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
      </listener>
 
      <servlet>
        <servlet-name>CXFService</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
      </servlet>
 
      <servlet-mapping>
        <servlet-name>CXFService</servlet-name>
        <url-pattern>/cxf/*</url-pattern>
      </servlet-mapping>
  
    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>    
    </servlet>

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

 

 

由于在类中没有规定死到底使用json还是xml,所有在访问时可以在请求后面加.xml或.json来得到想要的格式

如:http://127.0.0.1:8080/jscxfweb/cxf/service/jiongshi/search.xml

 

 

转载于:https://www.cnblogs.com/benjiaren/p/3431500.html

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐