{"id":2375,"date":"2017-11-29T16:30:56","date_gmt":"2017-11-29T08:30:56","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=2375"},"modified":"2017-11-29T16:30:56","modified_gmt":"2017-11-29T08:30:56","slug":"jersey%e6%95%99%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=2375","title":{"rendered":"jersey\u6559\u7a0b"},"content":{"rendered":"<p>\n\tRESTful \u76f8\u4fe1\u5927\u5bb6\u5e76\u4e0d\u964c\u751f\u3002RESTful\u67b6\u6784\uff0c\u5c31\u662f\u76ee\u524d\u6700\u6d41\u884c\u7684\u4e00\u79cd\u4e92\u8054\u7f51\u8f6f\u4ef6\u67b6\u6784\u3002\u5b83\u7ed3\u6784\u6e05\u6670\u3001\u7b26\u5408\u6807\u51c6\u3001\u6613\u4e8e\u7406\u89e3\u3001\u6269\u5c55\u65b9\u4fbf\uff0c\u6240\u4ee5\u6b63\u5f97\u5230\u8d8a\u6765\u8d8a\u591a\u7f51\u7ad9\u7684\u91c7\u7528\u3002\u76ee\u524d\u53ef\u4ee5\u5b9e\u73b0RESTful\u67b6\u6784\u7684\u6846\u67b6\u4e5f\u4e1b\u51fa\u4e0d\u7a77\uff0c\u672c\u6587\u4e3b\u8981\u4ecb\u7ecd\u57fa\u4e8eJersey\u5b9e\u73b0\u7684RESTful\u67b6\u6784\u3002\n<\/p>\n<p>\n\tJersey RESTful \u6846\u67b6\u662f\u5f00\u6e90\u7684RESTful\u6846\u67b6, \u5b9e\u73b0\u4e86JAX-RS (JSR 311 &amp; JSR 339) \u89c4\u8303\u3002\u5b83\u6269\u5c55\u4e86JAX-RS \u53c2\u8003\u5b9e\u73b0\uff0c \u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u7279\u6027\u548c\u5de5\u5177\uff0c \u53ef\u4ee5\u8fdb\u4e00\u6b65\u5730\u7b80\u5316 RESTful service \u548c client \u5f00\u53d1\u3002\u5c3d\u7ba1\u76f8\u5bf9\u5e74\u8f7b\uff0c\u5b83\u5df2\u7ecf\u662f\u4e00\u4e2a\u4ea7\u54c1\u7ea7\u7684 RESTful service \u548c client \u6846\u67b6\u3002\u4e0eStruts\u7c7b\u4f3c\uff0c\u5b83\u540c\u6837\u53ef\u4ee5\u548chibernate,spring\u6846\u67b6\u6574\u5408\u3002\n<\/p>\n<p>\n\t\u4e8eStruts2+hibernate+spring\u6574\u5408\u5728\u5e02\u573a\u7684\u5360\u6709\u7387\u592a\u9ad8\uff0c\u6240\u4ee5\u5f88\u5c11\u4e00\u90e8\u5206\u4eba\u53bb\u5173\u6ce8Jersey\u3002\u6240\u4ee5\u7f51\u4e0a\u6709\u5173\u4e8eJersey\u7684\u4ecb\u7ecd\u5f88\u5c11\u3002\u4f46\u662f\u5b83\u786e\u5b9e\u662f\u4e00\u4e2a\u975e\u5e38\u4e0d\u9519\u7684\u6846\u67b6\u3002\u5bf9\u4e8e\u8bf7\u6c42\u5f0f\u670d\u52a1\uff0c\u5bf9\u4e8eGET,DELETE\u8bf7\u6c42\uff0c\u4f60\u751a\u81f3\u53ea\u9700\u8981\u7ed9\u51fa\u4e00\u4e2aURI\u5373\u53ef\u5b8c\u6210\u64cd\u4f5c\u3002\n<\/p>\n<p>\n\t\u4e3e\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a\u5982\u679c\u4f60\u60f3\u83b7\u5f97\u670d\u52a1\u5668\u6570\u636e\u5e93\u4e2d\u7684\u6240\u6709\u6570\u636e\uff1b\u90a3\u4e48\u4f60\u53ef\u4ee5\u5728\u6d4f\u89c8\u5668\u6216\u8005\u5229\u7528Ajax\u7684GET\u65b9\u6cd5\uff0c\u5c06\u8def\u5f84\u8bbe\u7f6e\u597d\uff1b\u4f8b\u5982\uff1alocalhost:8080\/Student(\u9879\u76ee\u540d\u79f0)\/studentinfo(\u9879\u76ee\u670d\u52a1\u603b\u4f53\u524d\u7f00)\/student(\u5904\u7406student\u5bf9\u8c61\u7684\u7b7e\u6ce8)\/getStudentInfo(\u6700\u540e\u524d\u7f00)\u3002\u8fd9\u6837\u5c31\u53ef\u4ee5\u83b7\u53d6\u6240\u6709\u5b66\u751f\u4fe1\u606f\u3002\u4f60\u53ef\u4ee5\u9009\u62e9GET\u83b7\u53d6\u7684\u6570\u636e\u7684\u8fd4\u56de\u7c7b\u578b\uff1aJSON,XML,TEXT_HTML(String)..\u83b7\u53d6\u4e4b\u540e\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7JS\u5c06\u8fd9\u4e9b\u6570\u636e\u585e\u5230html\u6216\u8005jsp\u9875\u9762\u4e0a\u3002\n<\/p>\n<p>\n\t\u4e0b\u9762\u6211\u4eec\u4f7f\u7528<a href=\"https:\/\/jersey.java.net\/download.html\" name=\"Jersey\" target=\"_blank\" title=\"Jersey\">Jersey 2.7<\/a>\uff0c\u6765\u5b9e\u73b0\u4e00\u4e2aHello World\u9879\u76ee\u3002\n<\/p>\n<p>\n\t\u7b2c\u4e00\u6b65\uff0c\u5728Eclipse \u4e2d\u521b\u5efa\u4e00\u4e2a Dynamic Web Project ,\u672c\u4f8b\u4e3a&ldquo;RestDemo&rdquo;\u3002\n<\/p>\n<p>\n\t\u7b2c\u4e8c\u6b65\uff0c\u89e3\u538b\u4e0b\u8f7d\u597d\u7684Jersey 2.7\u3002\u5c06api\u3001ext\u3001lib\u6587\u4ef6\u5939\u4e0b\u7684jar\u5305\u90fd\u653e\u5230\u9879\u76ee\u7684lib\u4e0b\u3002\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"Jersey \u76ee\u5f55\" src=\"http:\/\/d.hiphotos.bdimg.com\/album\/s%3D550%3Bq%3D90%3Bc%3Dxiangce%2C100%2C100\/sign=41a40894bc096b6385195e553c08f679\/f31fbe096b63f6242082f6b88544ebf81b4ca3aa.jpg?referer=dc3f5bcfaf3459829c9dd1a20d68&amp;x=.jpg\" style=\"width: 356px;height: 252px\" \/>\n<\/p>\n<p>\n\t\u8bb0\u4f4f\u662f\u6240\u6709\u7684jar\u5305\uff0c\u4e0d\u662f3\u4e2a\u6587\u4ef6\u5939\u3002\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"jersey\u6559\u7a0b\" src=\"http:\/\/d.hiphotos.bdimg.com\/album\/s%3D550%3Bq%3D90%3Bc%3Dxiangce%2C100%2C100\/sign=5011985bf503918fd3d13dcf610657aa\/63d0f703918fa0ec3bccabb6249759ee3d6ddb06.jpg?referer=1b02ce10013b5bb5e7c015ce251c&amp;x=.jpg\" style=\"width: 403px;height: 531px\" \/>\n<\/p>\n<p>\n\t\u7b2c\u4e09\u6b65\uff0c\u5728resources\u5305\u4e0b\u5efa\u4e00\u4e2aclass&ldquo;HelloResource&rdquo;\u3002\n<\/p>\n<pre class=\"brush:java;\">\r\npackage com.xttblog.rest.resources;  \r\n\r\nimport javax.ws.rs.GET;  \r\nimport javax.ws.rs.Path;  \r\nimport javax.ws.rs.Produces;  \r\nimport javax.ws.rs.PathParam;  \r\nimport javax.ws.rs.core.MediaType;  \r\n\r\n@Path(&quot;\/hello&quot;)  \r\npublic class HelloResource {  \r\n    @GET  \r\n    @Produces(MediaType.TEXT_PLAIN)  \r\n    public String sayHello() {  \r\n        return &quot;Hello World!&quot; ;  \r\n    }  \r\n    @GET  \r\n    @Path(&quot;\/{param}&quot;)    \r\n    @Produces(&quot;text\/plain;charset=UTF-8&quot;)  \r\n    public String sayHelloToUTF8(@PathParam(&quot;param&quot;) String username) {  \r\n        return &quot;Hello &quot; + username;  \r\n    } \r\n}  <\/pre>\n<p>\n\t\u7b2c\u56db\u6b65\uff0c\u4fee\u6539web.xml\uff0c\u6dfb\u52a0\u57fa\u4e8eServlet\u7684\u90e8\u7f72\u3002\n<\/p>\n<pre class=\"brush:xml;\">\r\n&lt;servlet&gt;    \r\n    &lt;servlet-name&gt;RESTService&lt;\/servlet-name&gt;  \r\n\t&lt;servlet-class&gt;org.glassfish.jersey.servlet.ServletContainer&lt;\/servlet-class&gt;  \r\n\t&lt;init-param&gt;    \r\n\t\t&lt;param-name&gt;jersey.config.server.provider.packages&lt;\/param-name&gt;  \r\n        &lt;param-value&gt;com.xttblog.rest.resources&lt;\/param-value&gt;  \r\n\t&lt;\/init-param&gt;  \r\n\t&lt;load-on-startup&gt;1&lt;\/load-on-startup&gt;  \r\n&lt;\/servlet&gt;  \r\n&lt;servlet-mapping&gt;  \r\n\t&lt;servlet-name&gt;RESTService&lt;\/servlet-name&gt;  \r\n\t&lt;url-pattern&gt;\/xttblog\/*&lt;\/url-pattern&gt;  \r\n&lt;\/servlet-mapping&gt;<\/pre>\n<p>\n\t\u6700\u540e\uff0c\u6211\u4eec\u90e8\u7f72\u9879\u76ee\uff0c\u8f93\u5165http:\/\/localhost:8089\/RestDemo\/xttblog\/hello\uff0c\u8fdb\u884c\u8bbf\u95ee\u3002\u5c31\u8f93\u51faHello World\u4e86\u3002\n<\/p>\n<h2>\n\t\u53c2\u8003\u8d44\u6599<br \/>\n<\/h2>\n<ul>\n<li>\n\t\t<a href=\"https:\/\/jersey.github.io\/\" name=\"RESTful Web Services in Java\" target=\"_blank\" title=\"RESTful Web Services in Java\">RESTful Web Services in Java<\/a>\n\t<\/li>\n<li>\n\t\t<a href=\"http:\/\/www.ruanyifeng.com\/blog\/2014\/05\/restful_api.html\" name=\"RESTful API \u8bbe\u8ba1\u6307\u5357\" target=\"_blank\" title=\"RESTful API \u8bbe\u8ba1\u6307\u5357\">RESTful API \u8bbe\u8ba1\u6307\u5357<\/a>\n\t<\/li>\n<li>\n\t\t<a href=\"http:\/\/www.ruanyifeng.com\/blog\/2011\/09\/restful.html\" name=\"\u7406\u89e3RESTful\u67b6\u6784\" target=\"_blank\" title=\"\u7406\u89e3RESTful\u67b6\u6784\">\u7406\u89e3RESTful\u67b6\u6784<\/a>\n\t<\/li>\n<\/ul>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=2375\">jersey\u6559\u7a0b<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>RESTful \u76f8\u4fe1\u5927\u5bb6\u5e76\u4e0d\u964c\u751f\u3002RESTful\u67b6\u6784\uff0c\u5c31\u662f\u76ee\u524d\u6700\u6d41\u884c\u7684\u4e00\u79cd\u4e92\u8054\u7f51\u8f6f\u4ef6\u67b6\u6784\u3002\u5b83\u7ed3\u6784\u6e05\u6670\u3001\u7b26\u5408\u6807\u51c6 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[69,1587,604,490],"class_list":["post-2375","post","type-post","status-publish","format-standard","hentry","category-java","tag-java","tag-jersey","tag-restfull","tag-springmvcrestful"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/2375","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2375"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/2375\/revisions"}],"predecessor-version":[{"id":2376,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/2375\/revisions\/2376"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2375"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2375"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2375"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}