REST

Download pdf file from JAX-RS

<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;" trbidi&equals;"on">In this tutorial we will discuss about download <i><b>pdf <&sol;b><&sol;i>files from <i><b>JAX-RS<&sol;b><&sol;i>&comma; for user to download a file&comma; annotate the method with <i><b>&commat;Produces&lpar;&&num;8220&semi;application&sol;pdf&&num;8221&semi;&rpar;<&sol;b><&sol;i> &colon;<&sol;p>&NewLine;<ol>&NewLine;<li>Put<i><b> &commat;Produces&lpar;&&num;8220&semi;application&sol;pdf&&num;8221&semi;&rpar;<&sol;b><&sol;i> on service method&comma; with a Response return type&period; It means the output is a pdf file&period;<&sol;li>&NewLine;<li>Set <i><b>&&num;8220&semi;Content-Disposition&&num;8221&semi; <&sol;b><&sol;i>in Response header to tell browser pop up a download box for user to download&period;<&sol;li>&NewLine;<&sol;ol>&NewLine;<p>&NewLine; <b>1&period; Download PDF file in <i>JAX-RS<&sol;i><&sol;b><&sol;p>&NewLine;<p> Full example to download an <i><b>pdf <&sol;b><&sol;i>file from <i><b>JAX-RS<&sol;b><&sol;i>&period;<&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">package com&period;dineshonjava&period;ws&period;rest&semi; &NewLine; &NewLine;import java&period;io&period;File&semi; &NewLine; &NewLine;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;Produces&semi; &NewLine;import javax&period;ws&period;rs&period;core&period;Response&semi; &NewLine;import javax&period;ws&period;rs&period;core&period;Response&period;ResponseBuilder&semi; &NewLine; &NewLine;&sol;&ast;&ast; &NewLine; &ast; &commat;author Dinesh Rajput &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;&commat;Path&lpar;"&sol;pdf"&rpar; &NewLine;public class PdfFileService &lbrace; &NewLine; private static final String PDF&lowbar;PATH &equals; "d&colon;mongodb&period;pdf"&semi; &NewLine; &NewLine; &commat;GET &NewLine; &commat;Path&lpar;"&sol;get"&rpar; &NewLine; &commat;Produces&lpar;"application&sol;pdf"&rpar; &NewLine; public Response getSalarySlipFile&lpar;&rpar; &lbrace; &NewLine; &NewLine; File file &equals; new File&lpar;PDF&lowbar;PATH&rpar;&semi; &NewLine; &NewLine; ResponseBuilder response &equals; Response&period;ok&lpar;&lpar;Object&rpar; file&rpar;&semi; &NewLine; response&period;header&lpar;"Content-Disposition"&comma; &NewLine; "attachment&semi; filename&equals;"mongodb&lowbar;pdf&lowbar;file&period;pdf""&rpar;&semi; &NewLine; return response&period;build&lpar;&rpar;&semi; &NewLine; &NewLine; &rcub; &NewLine;&rcub; &NewLine; &NewLine;<&sol;pre>&NewLine;<div align&equals;'center' id&equals;"ads-id"><&sol;div>&NewLine;<p><b>2&period; Test<&sol;b><&sol;p>&NewLine;<p> Deploy above <i><b>JAX-RS<&sol;b><&sol;i> service&comma; access this URI pattern &colon; &&num;8220&semi;<i><b>http&colon;&sol;&sol;localhost&colon;8181&sol;sdnext&sol;doj&sol;pdf&sol;get<&sol;b><&sol;i>&&num;8220&semi;&period;<&sol;p>&NewLine;<p> Figure &colon; PDF file &&num;8220&semi;<b>d&colon;mongodb&period;pdf<&sol;b>&&num;8221&semi; from server is prompt for user to download&comma; with a new <b>pdf <&sol;b>file name <b>&&num;8220&semi;mongodb&lowbar;pdf&lowbar;file&period;pdf&&num;8221&semi;<&sol;b><&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img border&equals;"0" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;06&sol;downloadpdf&period;png"&sol;><&sol;div>&NewLine;<p>&NewLine;<b><br &sol;>&NewLine;<&sol;b> <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;Download&percnt;20pdf&percnt;20file&percnt;20from&percnt;20JAX-RS&period;zip&quest;attredirects&equals;0&amp&semi;d&equals;1" target&equals;"&lowbar;blank">Download pdf file from JAX-RS&period;zip<&sol;a><&sol;b><&sol;p>&NewLine;<p>&NewLine;<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;download-image-file-from-jax-rs&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;download-excel-file-from-jax-rs&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;download-image-file-from-jax-rs&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;download-excel-file-from-jax-rs&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; '416'&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