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