Archive

Posts Tagged ‘spring-mvc’

Spring MVC: @ResponseBody and URL Extensions

December 29th, 2016 No comments

If you use Spring MVC to develop web services then you most likely have run into a limitation with @ResponseBody.  Specifically, Spring will automatically marshal the return value of an annotated method to XML, JSON, etc. depending on what the client requests.  Unfortunately, Spring only considers the Accepts header of the client.  While this works well enough when using REST clients, it is particularly difficult to work with in web browsers.  In this article I’ll show you how to extend @ResponseBody to support more advanced forms of content negotiation including analyzing the URL’s extension.

Updated 9/22/11 — Fixed a bug where CNRB was not setting an Accept header if the client did not already include one. Many thanks to Leone Parise for finding the bug, reporting it, and submitting the patch.

Ideally, we would like Spring to consider the URL’s extension when determining what format should be used.  For example, /pets.xml should return a list of pets in an XML format while /pets.json should return a list of pets in JSON format.

Until support is officially added to the Spring Framework, I would recommend using my ContentNegotatingResponseBody class.  To do so, put the class in your class path and add the following being registration to your Spring configuration:

<bean class='me.tedyoung.blog.contentnegotatingresponsebody.ContentNegotatingResponseBody'>
	<property name="mediaTypes">
		<map>
			<entry key="atom" value="application/atom+xml"/>
			<entry key="xml" value="application/xml"/>
			<entry key="json" value="application/json"/>
			<entry key="pdf" value="application/pdf"/>
		</map>
	</property>
</bean>

You will likely have to modify the class’s package from this example.

The mediaTypes property maps file extensions to content types.  Internally, ContentNegotatingResponseBody will look at the file extension of the URL.  If the file extension is found in the list of mediaTypes, it will set the content type to the corresponding value.  Otherwise, it will fall back to Spring’s default behavior of analyzing the Accepts header.

Feel free to use this in your own applications.  The exact copyright can be found here.

Practical Spring MVC Part 1: The Basics

December 23rd, 2016 No comments

In this fast-paced, demo-driven series, I will take you on an thrilling tour of Spring MVC.  As opposed to “pet clinic” style demonstrations, I will make use of sensible solutions to genuine-globe issues in order to demonstrate the breadth of functionality supplied by Spring MVC.  This article is a perfect match for anyone seeking for a fast overview of Spring MVC and its capabilities.  Moreover, total code is included for those who want to a stick to along closely and explore the concepts presented.

Read more…

Practical Spring MVC Part 2: Forms

December 21st, 2016 No comments

In this quick-paced, demo-driven series, I will take you on an fascinating tour of Spring MVC. Unlike “pet clinic” style demonstrations, I will make use of practical solutions to genuine-world issues in order to demonstrate the breadth of functionality offered by Spring MVC. This write-up is a ideal match for anyone looking for a fast overview of Spring MVC and its capabilities. Moreover, total code is included for those who wish to a stick to along closely and explore the ideas presented.

Read more…

Practical Spring MVC Part 3: The Presentation Layer

December 19th, 2016 No comments

In this quickly-paced, demo-driven series, I will take you on an exciting tour of Spring MVC. Unlike “pet clinic” style demonstrations, I will make use of practical solutions to actual-world issues in order to demonstrate the breadth of functionality provided by Spring MVC. This post is a excellent fit for any person looking for a fast overview of Spring MVC and its capabilities. Moreover, full code is incorporated for those who wish to a follow along closely and discover the ideas presented.

Read more…

Practical Spring MVC Part 4: Web Services

December 17th, 2016 No comments

In this fast-paced, demo-driven series, I will take you on an exciting tour of Spring MVC. Unlike “pet clinic” style demonstrations, I will make use of practical solutions to real-world problems in order to demonstrate the breadth of functionality offered by Spring MVC. This article is a perfect fit for anyone looking for a quick overview of Spring MVC and its capabilities. Furthermore, complete code is included for those who wish to a follow along closely and explore the concepts presented.

Read more…

Practical Spring MVC Part 5: Sessions

December 15th, 2016 No comments

In this fast-paced, demo-driven series, I will take you on an exciting tour of Spring MVC. Unlike “pet clinic” style demonstrations, I will make use of practical solutions to real-world problems in order to demonstrate the breadth of functionality offered by Spring MVC. This article is a perfect fit for anyone looking for a quick overview of Spring MVC and its capabilities. Furthermore, complete code is included for those who wish to a follow along closely and explore the concepts presented.

Read more…

Lecture – Practical Spring MVC

December 13th, 2016 No comments

For these of you who have attended my Sensible Spring MVC lecture, please discover a publication of the lecture, and supply code beneath. If you are interested in this lecture, or would like to present on yet another subject, please feel free of charge to get in touch with me.

Categories: Presentations Tags: ,