Archive

Posts Tagged ‘spring’

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.

Unicode Web Apps

December 27th, 2016 No comments

Are you attempting to help foreign languages in your internet application and find ?? instead of 你好?  Do your customers get a bunch of funny vowels (Ãă) when they try to past “smart” quotes in your content management system (a problem I termed Irritable Vowel Syndrome)?  The problem is broken Unicode help someplace in your stack.

With the prevalence and lengthy history of Unicode, 1 may well be shocked that it is nonetheless not trivial to develop a internet application that supports unicode!  Let me show you how.

Read more…

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: ,

Custom User Types with JPA and Spring

December 9th, 2016 No comments

Thanks to Spring and JPA, one can easily and intuitively bind plain old Java objects to HTML forms, HTTP requests, and database tables. This beloved combination eliminates the tedium and droves of boiler plate code associated with developing data management applications. These tools support all Java primitives, wrappers, and even several high-level types natively. Fortunately, it is easy to add support for your own custom types.

Read more…

Spring: Auto Detecting Type Converters

December 5th, 2016 No comments

So, you have been enjoying Spring’s type converter system. However, like me, you are lazy and do not want to register all of your type converters manually. Using JavaConfig, is trivial to have the container autodetect and register type converters for you.

Read more…

Categories: Software Development Tags: