jersey 实现restful经验总结(待续)


/**
 * 实作例子

 * @author jinlong
 *
 */
//这个标签可以实现依赖注入
@Component

//路径,网址+"/game"
@Path("/game")
public class GameResource {

 @Context
 private UriInfo context;
 private IGameService gameService;
 
 public GameResource(){
  super();
 }
 /**
  * 1.查找全部游戏
  * MediaType.APPLICATION_JSON
  * 设置返回值为Json类型
  * @return
  */
 @GET
 @Produces({MediaType.APPLICATION_JSON})
 public GameGrid getGames(){
  GameGrid gameGrid = new GameGrid();
  try {
   gameGrid.setData(gameService.readGames());
   gameGrid.setSuccess(true);
   gameGrid.setMessage("成功");
  } catch (Exception e) {
   gameGrid.setSuccess(false);
   gameGrid.setMessage("失败");
  }
  return gameGrid;
 }
 /**
  * 设置返回值为xml类型
  * 返回的为application/xml
  * @return
  */
 @GET
 @Path("/xml")
 @Produces("application/xml")
 public GameGrid getGamesXML(){
  GameGrid gameGrid = new GameGrid();
  try {
   gameGrid.setData(gameService.readGames());
   gameGrid.setSuccess(true);
   gameGrid.setMessage("成功");
  } catch (Exception e) {
   gameGrid.setSuccess(false);
   gameGrid.setMessage("失败");
  }
  return gameGrid;
 }
 /**
  * 这个方法实现了jquery 的jsonp方式
  * 只是将数据转换为json,还需要一个json-lib
  * @return
  */
 @GET
 @Path("/jsonp")
 @Produces({MediaType.TEXT_PLAIN})
 public String getGames4jsonp(){
  GameGrid gameGrid = new GameGrid();
  try {
   gameGrid.setData(gameService.readGames());
   gameGrid.setSuccess(true);
   gameGrid.setMessage("成功");
  } catch (Exception e) {
   gameGrid.setSuccess(false);
   gameGrid.setMessage("失败");
  }
  List<String> list = context.getQueryParameters().get("jsoncallback");
  String jsoncallbackFunction = list.get(0);

  return jsoncallbackFunction+"('abc')";
 }
 /**
  * 第二个方法
  * 根据ID查找游戏
  * @param id
  * @return
  */
 @GET
 @Path("/{id}")
 @Produces({MediaType.APPLICATION_JSON})
 public  GameGrid getGame(@PathParam("id") int id){
  GameGrid gameGrid = new GameGrid();
  try {
   List<GameInfo> gameInfo = new ArrayList<GameInfo>();
   gameInfo.add(gameService.readGame(id));
   gameGrid.setData(gameInfo);
   gameGrid.setSuccess(true);
   gameGrid.setMessage("成功");
  } catch (Exception e) {
   gameGrid.setSuccess(false);
   gameGrid.setMessage("失败");
  }
  return gameGrid;
 }
 
 /**
  * 第三个方法,创建游戏
  * @param game
  * @return
  */
 @POST
 @Produces( { MediaType.APPLICATION_JSON})
 @Consumes({MediaType.APPLICATION_JSON})
 public GameGrid postGame(GameGrid grid){
  GameGrid gameGrid = new GameGrid();
  try {
  GameInfo game = gameService.saveGame(grid.getData().get(0));
   //gameGrid.getData().get(0).setId(game.getId());
   gameGrid.setMessage("游戏添加成功");
   gameGrid.setSuccess(true);
  } catch (Exception e) {
   gameGrid.setMessage("游戏添加失败");
   gameGrid.setSuccess(false);
   e.printStackTrace();
  }
  return gameGrid;
 }
 
 @POST
 @Path("/json")
 @Produces( { MediaType.APPLICATION_JSON})
 @Consumes({MediaType.APPLICATION_JSON})
 public GameGrid postGame(GameInfo game){
  GameGrid gameGrid = new GameGrid();
  List<GameInfo> list = new ArrayList<GameInfo>();
  try {
   GameInfo gamenew = gameService.saveGame(game);
   list.add(gamenew);
   gameGrid.setData(list);
   gameGrid.setMessage("游戏添加成功");
   gameGrid.setSuccess(true);
  } catch (Exception e) {
   gameGrid.setMessage("游戏添加失败");
   gameGrid.setSuccess(false);
  }
  return gameGrid;
 }
 
 /**
  * 接受GameInfo JSON格式数据
  * @param grid
  * @return
  */
 /*@POST
 @Produces( { MediaType.APPLICATION_JSON})
 @Consumes({MediaType.APPLICATION_JSON})
 public GameGrid postGame(GameInfo game){
  GameGrid gameGrid = new GameGrid();
  List<GameInfo> list = new ArrayList<GameInfo>();
  try {
   list.add(gameService.saveGame(game));
   gameGrid.setData(list);
   gameGrid.setMessage("游戏添加成功");
   gameGrid.setSuccess(true);
  } catch (Exception e) {
   gameGrid.setMessage("游戏添加失败");
   gameGrid.setSuccess(false);
  }
  return gameGrid;
 }*/
 /**
  * 第四个方法,修改
  */
 @PUT
 @Path("/{id}")
 //返回json类型
 @Produces({MediaType.APPLICATION_JSON})
 //接收json类型
 @Consumes({MediaType.APPLICATION_JSON})
 public GameGrid putUser(@PathParam("id") int id,GameGrid gameGrid){
  GameInfo game = gameGrid.getData().get(0);
  game.setId(id);
  try {
   gameService.saveGame(game);
   gameGrid.setMessage("修改游戏成功");
   gameGrid.setSuccess(true);
  } catch (Exception e) {
   gameGrid.setMessage("修改游戏失败");
   gameGrid.setSuccess(false);
  }
  return gameGrid;
 }
 
 @DELETE
 @Path("/{id}")
 @Produces({MediaType.APPLICATION_JSON})
 public GameGrid deleteGame(@PathParam("id") int id){
  GameGrid gameGrid = new GameGrid();
  try {
   gameService.deleteGame(id);
   gameGrid.setMessage("删除成功");
   gameGrid.setSuccess(true);
  } catch (Exception e) {
   gameGrid.setMessage("删除失败");
   gameGrid.setSuccess(false);
  }
  return gameGrid;
 }
 
 //好好做事,别抱怨了,没用。
 //该干什么,就干什么。
 //该干什么,就干什么。
 public UriInfo getContext() {
  return context;
 }
 public void setContext(UriInfo context) {
  this.context = context;
 }
 public IGameService getGameService() {
  return gameService;
 }
 public void setGameService(IGameService gameService) {
  this.gameService = gameService;
 }
}

 

------------------------注意事项----------------------

1.@Get是访问方式注解

2.@Path是路径注解

3. @Produces( { MediaType.APPLICATION_JSON}) 返回json类型数据
 @Consumes({MediaType.APPLICATION_JSON})  接受Json数据并将其转换为java对象
差不多就这些吧。

<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_4.xsd"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_4.xsd"
 id="WebApp_ID" version="2.4">

 <display-name>Telestone NMS Server</display-name>

 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:META-INF/applicationContext*.xml</param-value>
 </context-param>
 
Spring 容器配置 -->
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 
 <!-- Jersey 配置 -->
 <servlet>
  <servlet-name>Jersey Spring Web Application</servlet-name>
  <servlet-class>
   com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>

 <servlet-mapping>
  <servlet-name>Jersey Spring Web Application</servlet-name>
  <url-pattern>/*</url-pattern>
 </servlet-mapping>

</web-app>

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值