Jersey RESTful WebService框架学习(二)使用@PathParam
@PathParamuri路径参数写在方法的参数中,获得请求路径参数。比如:@PathParam("username") String userName
前端请求:
后端接收:
效果:
前端请求:
<!DOCTYPE html> <html ng-controller="PathParam"> <head> <title>@PathParam</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <script type="text/javascript" src="../plugins/angular/angular.js"></script> </head> <body> <div ng-click="init()">获取数据</div> <br> </body> <script type="text/javascript"> angular.module("@PathParam.html", []).controller("PathParam", function($scope, $http) { $scope.init = function() { $http({ method : 'get', //拼装uri路径参数 url : "/Jersey/api/1.0/my/first/1" }).success(function(data) { alert(angular.toJson(data)); }); }; }); angular.bootstrap(document, ['@PathParam.html']); </script> </html>
后端接收:
package com.lx.api; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; @Path("/my") public class TestAPI { @GET @Path("/first/{id}") @Produces({ MediaType.APPLICATION_JSON + ";charset=UTF-8" }) public String my(@PathParam(value = "id") String id) { System.out.println("我的第一个jersey程序"); return "{\"id\":\""+id+"\"}"; }
效果: