REST

JAX-RS @Path URI matching example

<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;" trbidi&equals;"on">In this <b>JAX-RS<&sol;b> tutorial &comma; you can use <b>&commat;Path<&sol;b> to bind <b>URI <&sol;b>pattern to a Java method&period; See following examples to show you how it works&period;<&sol;p>&NewLine;<p> <b>1&period; Normal URI Matching<&sol;b><br &sol;>&NewLine;See normal URI matching with <b>&commat;Path<&sol;b> annotation<&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">import javax&period;ws&period;rs&period;GET&semi; &NewLine;import javax&period;ws&period;rs&period;Path&semi; &NewLine;import javax&period;ws&period;rs&period;core&period;Response&semi; &NewLine; &NewLine;&commat;Path&lpar;"&sol;employees"&rpar; &NewLine;public class EmployeeRestService &lbrace; &NewLine; &commat;GET &NewLine; public Response getEmployee&lpar;&rpar; &lbrace; &NewLine; return Response&period;status&lpar;200&rpar;&period;entity&lpar;"getEmployee is called"&rpar;&period;build&lpar;&rpar;&semi; &NewLine; &rcub; &NewLine; &commat;GET &NewLine; &commat;Path&lpar;"&sol;manager"&rpar; &NewLine; public Response getEmployeeManager&lpar;&rpar; &lbrace; &NewLine; return Response&period;status&lpar;200&rpar;&period;entity&lpar;"getEmployeeManageris called"&rpar;&period;build&lpar;&rpar;&semi; &NewLine; &rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<div align&equals;"center" id&equals;"ads-id"><&sol;div>&NewLine;<p><i><b>URI pattern &colon; &&num;8220&semi;&sol;employees&&num;8221&semi;<&sol;b><&sol;i><&sol;p>&NewLine;<p><b>getEmployee is called<&sol;b><br &sol;>&NewLine;<b><br &sol;>&NewLine;<&sol;b> <i><b>URI pattern &colon; &&num;8220&semi;&sol;employees&sol;manager&&num;8221&semi;<&sol;b><&sol;i><&sol;p>&NewLine;<p><b>getEmployeeManageris is called<&sol;b><&sol;p>&NewLine;<p><b>2&period; URI Matching and Parameter<&sol;b><br &sol;>&NewLine;The value within an open brace <b>&&num;8220&semi;&lbrace;&&num;8221&semi; and close brace &&num;8220&semi;&rcub;&&num;8221&semi;<&sol;b>&comma; is represents a parameter&comma; and can be access with <b>&commat;PathParam&period;<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">import javax&period;ws&period;rs&period;GET&semi; &NewLine;import javax&period;ws&period;rs&period;Path&semi; &NewLine;import javax&period;ws&period;rs&period;PathParam&semi; &NewLine;import javax&period;ws&period;rs&period;core&period;Response&semi; &NewLine; &NewLine;&commat;Path&lpar;"&sol;employees"&rpar; &NewLine;public class EmployeeRestService &lbrace; &NewLine; &NewLine; &commat;GET &NewLine; &commat;Path&lpar;"&lbrace;employeeName&rcub;"&rpar; &NewLine; public Response getEmployeeByName&lpar;&commat;PathParam&lpar;"employeeName"&rpar; String employeeName&rpar; &lbrace; &NewLine; &NewLine; return Response&period;status&lpar;200&rpar; &NewLine; &period;entity&lpar;"getEmployeeByName is called&comma; employee name&colon; " &plus; employeeName&rpar;&period;build&lpar;&rpar;&semi; &NewLine; &NewLine; &rcub; &NewLine; &rcub; &NewLine;<&sol;pre>&NewLine;<p>&NewLine;<i><b>URI Pattern &colon; &&num;8220&semi;&sol;employees&sol;dinesh&&num;8221&semi;<&sol;b><&sol;i><&sol;p>&NewLine;<p><b>getEmployeeByName is called&comma; name &colon; dinesh<&sol;b><&sol;p>&NewLine;<p><i><b>URI Pattern &colon; &&num;8220&semi;&sol;employees&sol;sweety&&num;8221&semi;<&sol;b><&sol;i><br &sol;>&NewLine;<b><br &sol;>&NewLine;<&sol;b> <b>getEmployeeByName is called&comma; name &colon; sweety<&sol;b><&sol;p>&NewLine;<p> <b>3&period; URI Matching and Regular Expression<&sol;b><br &sol;>&NewLine;<b>&commat;Path<&sol;b> support complex URI matching with regular expression&comma; via following expression &colon;<&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">&lbrace;" variable-name &lbrack; "&colon;" regular-expression &rsqb; "&rcub; &NewLine;<&sol;pre>&NewLine;<pre class&equals;"highlight" name&equals;"code">import javax&period;ws&period;rs&period;GET&semi; &NewLine;import javax&period;ws&period;rs&period;Path&semi; &NewLine;import javax&period;ws&period;rs&period;PathParam&semi; &NewLine;import javax&period;ws&period;rs&period;core&period;Response&semi; &NewLine; &NewLine;&commat;Path&lpar;"&sol;employees"&rpar; &NewLine;public class EmployeeRestService &lbrace; &NewLine; &NewLine;&commat;GET &NewLine;&commat;Path&lpar;"&lbrace;employeeId &colon; d&plus;&rcub;"&rpar; &sol;&sol;support digit only &NewLine;public Response getEmployeeByEmpId&lpar;&commat;PathParam&lpar;"employeeId"&rpar; String employeeId&rpar; &lbrace; &NewLine; &NewLine;return Response&period;status&lpar;200&rpar;&period;entity&lpar;"getEmployeeByEmpId is called&comma; employeeId &colon; " &plus; employeeId&rpar;&period;build&lpar;&rpar;&semi; &NewLine; &NewLine;&rcub; &NewLine; &NewLine;&commat;GET &NewLine;&commat;Path&lpar;"&sol;employeeName&sol;&lbrace;employeeName &colon; &lbrack;a-zA-Z&rsqb;&lbrack;a-zA-Z&lowbar;0-9&rsqb;&rcub;"&rpar; &NewLine;public Response getEmployeeByEmployeeName&lpar;&commat;PathParam&lpar;"employeeName"&rpar; String employeeName&rpar; &lbrace; &NewLine; &NewLine;return Response&period;status&lpar;200&rpar; &NewLine;&period;entity&lpar;"getEmployeeByEmployeeName is called&comma; employeeName &colon; " &plus; employeeName&rpar;&period;build&lpar;&rpar;&semi; &NewLine; &NewLine;&rcub; &NewLine; &NewLine;&commat;GET &NewLine;&commat;Path&lpar;"&sol;salary&sol;&lbrace;sapid &colon; d&plus;&rcub;"&rpar; &NewLine;public Response getEmployeeSalaryBySapId&lpar;&commat;PathParam&lpar;"sapid"&rpar; String sapid&rpar; &lbrace; &NewLine; &NewLine;return Response&period;status&lpar;200&rpar; &NewLine;&period;entity&lpar;"getEmployeeSalaryBySapId is called&comma; sapid &colon; " &plus; sapid&rpar;&period;build&lpar;&rpar;&semi; &NewLine; &NewLine;&rcub; &NewLine; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p><i><b>URI Pattern &colon; &&num;8220&semi;&sol;employees&sol;1212&&num;8221&semi;<&sol;b><&sol;i><&sol;p>&NewLine;<p> getEmployeeByEmpId is called&comma; employeeId &colon; 1212<&sol;p>&NewLine;<p> <i><b>URI Pattern &colon; &&num;8220&semi;&sol;employees&sol;123456&&num;8221&semi;<&sol;b><&sol;i><&sol;p>&NewLine;<p> <b>getEmployeeByEmpId is called&comma; employeeId &colon; 123456<&sol;b><&sol;p>&NewLine;<p> URI Pattern &colon;<i><b> &&num;8220&semi;&sol;employees&sol;employeeName&sol;ddddd&&num;8221&semi;<&sol;b><&sol;i> &comma; failed&comma; don’t match &&num;8220&semi;&lbrack;a-zA-Z&rsqb;&lbrack;a-zA-Z&lowbar;0-9&rsqb;&&num;8221&semi;&comma; first character need &&num;8220&semi;&lbrack;a-zA-Z&rsqb;&&num;8221&semi;&comma; second character need &&num;8220&semi;&lbrack;a-zA-Z&lowbar;0-9&rsqb;&&num;8221&semi;&period;<&sol;p>&NewLine;<p> <b>Could not find resource for relative &colon; &sol;employees&sol;employeeName&sol;ddddd<&sol;b><&sol;p>&NewLine;<p> <i><b>URI Pattern &colon; &&num;8220&semi;&sol;employees&sol;employeeName&sol;d5&&num;8221&semi;<&sol;b><&sol;i><&sol;p>&NewLine;<p> <b>getEmployeeByEmployeeName is called&comma; employeeName &colon; d5<&sol;b><br &sol;>&NewLine;<i><b><br &sol;>&NewLine;<&sol;b><&sol;i> <i><b>URI Pattern &colon; &&num;8220&semi;employees&sol;salary&sol;1212&&num;8221&semi;<&sol;b><&sol;i><&sol;p>&NewLine;<p> <b>getEmployeeSalaryBySapId is called&comma; sapid&colon; 1212<&sol;b><&sol;p>&NewLine;<p><b>Download SourceCode<&sol;b><br &sol;>&NewLine;<b><a href&equals;"https&colon;&sol;&sol;sites&period;google&period;com&sol;a&sol;dineshonjava&period;com&sol;dineshonjava&sol;dineshonjava&sol;JAX-RS&percnt;20&percnt;40Path&percnt;20URI&percnt;20matching&percnt;20example&period;zip&quest;attredirects&equals;0&amp&semi;d&equals;1" target&equals;"&lowbar;blank">JAX-RS &commat;Path URI matching example&period;zip<&sol;a><&sol;b><&sol;p>&NewLine;<p><i><b>References<&sol;b><&sol;i><br &sol;>&NewLine;1&period;<b> <i><a href&equals;"http&colon;&sol;&sol;docs&period;oracle&period;com&sol;javaee&sol;6&sol;tutorial&sol;doc&sol;giepu&period;html" target&equals;"&lowbar;blank">JAVA REST Web Services<&sol;a><&sol;i><&sol;b><br &sol;>&NewLine;2&period; <i><b><a href&equals;"http&colon;&sol;&sol;en&period;wikipedia&period;org&sol;wiki&sol;Web&lowbar;service" target&equals;"&lowbar;blank">Wikipedia for REST Web Service<&sol;a><&sol;b><&sol;i><&sol;p>&NewLine;<p>&nbsp&semi; <&sol;p>&NewLine;<div style&equals;"background-color&colon; pink&semi; border-width&colon; thin&semi; text-align&colon; center&semi;"><b>&lt&semi;&lt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;using-jax-rs-with-jaxb&sol;">Previous <&sol;a>&lt&semi;&lt&semi;&nbsp&semi;&nbsp&semi; &vert;&vert; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;jax-rs-web-service-tutorial&sol;">Index <&sol;a>&vert;&vert; &nbsp&semi; &gt&semi;&gt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;jax-rs-pathparam-example&sol;">Next<&sol;a> &gt&semi;&gt&semi;<&sol;b><&sol;div>&NewLine;<p>&NewLine;<&sol;div>&NewLine;<div class&equals;"wp-post-navigation"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-pre"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;using-jax-rs-with-jaxb&sol;">Previous<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-next"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;jax-rs-pathparam-example&sol;">Next<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;<&sol;div>&NewLine;<script type&equals;"text&sol;javascript">&NewLine;jQuery&lpar;document&rpar;&period;ready&lpar;function&lpar;&dollar;&rpar; &lbrace;&NewLine; &dollar;&period;post&lpar;'https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-admin&sol;admin-ajax&period;php'&comma; &lbrace;action&colon; 'mts&lowbar;view&lowbar;count'&comma; id&colon; '424'&rcub;&rpar;&semi;&NewLine;&rcub;&rpar;&semi;&NewLine;<&sol;script>

Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

4 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

4 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

4 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

4 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

4 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

4 years ago