Enterprise Java

Spring MVC: REST application with CNVR vol. 1

Not so long time ago I have read an article written by Paul Chapman about Content Negotiating View Resolver (CNVR). That post on a Spring Framework Blog inspired me to investigate this framework’s area. As a result I have developed a sample REST application based on Spring MVC with CNVR. The application demonstrates a basic flow of a REST service – creation, deletion, reading and edition of an entity.

Spring Framework supports REST services for a long time, earlier you could develop some service using Message Converters. In Spring 3.2 all this stuff become more easier in configuration and development. So lets stop talking because I’m going to show a basic setup and exploitation of Spring REST service with CNVR.

A basic idea of CNVR is to define which kind of representation form for a resource give back to a client depending on information which CNVR gets from the client’s request. You can ask me: what is that information in the request which can impact on CNVR decision? The answer is simple:

  • URL sufix (e.g. .xml, .json, .html etc
  • URL parameter (format by default)
  • HTTP Accept header property

Here is an illustration of high level CNVR workflow:


For the more information I recommend to read a full article of Paul Chapman.

Setup the Spring MVC REST project with CNVR

I will work with a maven project, as always I will provide a link to a project’s GitHub repository. Here is a screenshot of entire project:


I have explained numerous times how to setup Dynamic Web Project in Eclipse, so now I will provide just source files with some short notes. You can find required maven dependencies below:


		<!-- DataBase libs -->
		<!-- Hibernate -->
		<!-- Spring -->
		<!-- CGLIB is required to process @Configuration classes -->
		<!-- Other -->
		<!-- CNVR resources -->

A full version of the pom.xml file you can find on GitHub. So let’s go ahead with the preparation. I will use MySQL as a database. And I need to create a following table in it:

CREATE TABLE `smartphones` (
  `producer` varchar(20) NOT NULL,
  `model` varchar(20) NOT NULL,
  `price` double NOT NULL,
  PRIMARY KEY (`id`)

Now we need appropriate java object which will represent the smartphones table:

public class Smartphone {

	private Integer id;

	private String producer;

	private String model;

	private double price;

	 * Method updates already existed {@link Smartphone} object with values from the inputed argument.
	 * @param sPhone - Object which contains new Smartphone values.
	 * @return {@link Smartphone} object to which this method applied.
	public Smartphone update(Smartphone sPhone) {
		this.producer = sPhone.producer;
		this.model = sPhone.model;
		this.price = sPhone.price;
		return this;

	public String toString() {
		return producer+": "+model+" with price "+price;

	public Integer getId() {
		return id;

	public void setId(Integer id) {
		this.id = id;

	public String getProducer() {
		return producer;

	public void setProducer(String producer) {
		this.producer = producer;

	public String getModel() {
		return model;

	public void setModel(String model) {
		this.model = model;

	public double getPrice() {
		return price;

	public void setPrice(double price) {
		this.price = price;


The most part of preparation is done. Service and DAO layer is the last thing which I need to do. I’m going to use Spring Data for the DAO layer, and in the one of my previous posts I have made a detail review of its setup.

public interface SmartphoneRepository extends JpaRepository< Smartphone, Integer >{ }

And here are corresponding service interface and its implementation:

public interface SmartphoneService {

	public Smartphone create(Smartphone sp);
	public Smartphone get(Integer id);
	public List< Smartphone > getAll();
	public Smartphone update(Smartphone sp) throws SmartphoneNotFoundException;
	public Smartphone delete(Integer id) throws SmartphoneNotFoundException;


Service implementation:

public class SmartphoneServiceImpl implements SmartphoneService {

	private SmartphoneRepository smartphoneRepository;

	public Smartphone create(Smartphone sp) {
		return smartphoneRepository.save(sp);

	public Smartphone get(Integer id) {
		return smartphoneRepository.findOne(id);

	public List< Smartphone > getAll() {
		return smartphoneRepository.findAll();

	public Smartphone update(Smartphone sp) throws SmartphoneNotFoundException {
		Smartphone sPhoneToUpdate = get(sp.getId());
		if (sPhoneToUpdate == null)
			throw new SmartphoneNotFoundException(sp.getId().toString());
		return sPhoneToUpdate;

	public Smartphone delete(Integer id) throws SmartphoneNotFoundException {
		Smartphone sPhone = get(id);
		if (sPhone == null)
			throw new SmartphoneNotFoundException(id.toString());
		return sPhone;

In the end of setting up of the project let’s consider the “heart” of configurations: Initializer and WebAppConfig files.

public class WebAppConfig extends WebMvcConfigurerAdapter {

    private static final String PROPERTY_NAME_DATABASE_DRIVER = "db.driver";
    private static final String PROPERTY_NAME_DATABASE_PASSWORD = "db.password";
    private static final String PROPERTY_NAME_DATABASE_URL = "db.url";
    private static final String PROPERTY_NAME_DATABASE_USERNAME = "db.username";

    private static final String PROPERTY_NAME_HIBERNATE_DIALECT = "hibernate.dialect";
    private static final String PROPERTY_NAME_HIBERNATE_SHOW_SQL = "hibernate.show_sql";
    private static final String PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN = "entitymanager.packages.to.scan";

	private Environment env;

	public DataSource dataSource() {
		DriverManagerDataSource dataSource = new DriverManagerDataSource();


		return dataSource;

	public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
		LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();


		return entityManagerFactoryBean;

	private Properties hibProperties() {
		Properties properties = new Properties();
		return properties;	

	public JpaTransactionManager transactionManager() {
		JpaTransactionManager transactionManager = new JpaTransactionManager();
		return transactionManager;

	public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
			.mediaType("html", MediaType.TEXT_HTML)
			.mediaType("json", MediaType.APPLICATION_JSON)

	public ViewResolver contentNegotiatingViewResolver(
			ContentNegotiationManager manager) {

		List< ViewResolver > resolvers = new ArrayList< ViewResolver >();

		InternalResourceViewResolver r1 = new InternalResourceViewResolver();

		JsonViewResolver r2 = new JsonViewResolver();

		ContentNegotiatingViewResolver resolver = new ContentNegotiatingViewResolver();
	    return resolver;


	* View resolver for returning JSON in a view-based system. Always returns a
	* {@link MappingJacksonJsonView}.
	public class JsonViewResolver implements ViewResolver {
		public View resolveViewName(String viewName, Locale locale)
				throws Exception {
				MappingJacksonJsonView view = new MappingJacksonJsonView();
				return view;


Despite that the file is big enough I want to focus your attention just on several things. The first one is the JsonViewResolver inner class. It’s required for handling of JSON requests. Of course, it can be declared separatly of the WebAppConfig class and just be imported in it. But I decide to put it directly in the WebAppConfig to avoid a dispersal of attention. The second one is the configureContentNegotiation method. There I set options for a content negotiation view resolver. And finally in the contentNegotiatingViewResolver bean I have determined which view resolvers will be available in the my application.

public class Initializer implements WebApplicationInitializer {

	private static final String DISPATCHER_SERVLET_NAME = "dispatcher";

	public void onStartup(ServletContext servletContext) throws ServletException {
		AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();



		Dynamic servlet = servletContext.addServlet(DISPATCHER_SERVLET_NAME, new DispatcherServlet(ctx));


	private void registerHiddenHttpMethodFilter(ServletContext servletContext) {
		FilterRegistration.Dynamic fr = servletContext
				.addFilter("hiddenHttpMethodFilter", HiddenHttpMethodFilter.class);
				EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD), 


In the Initializer class there is just one important thing in context of this tutorial. It is registerHiddenHttpMethodFilter method. This method will help to deal with such HTTP methods as PUT and DELETE.

I hope you didn’t tired, because the most interesting will wait for you in the following part of this tutorial.

Reference: Spring MVC: REST application with CNVR vol. 1 from our JCG partner Alexey Zvolinskiy at the Fruzenshtein’s notes blog.

Alexey Zvolinskiy

Alexey is a test developer with solid experience in automation of web-applications using Java, TestNG and Selenium. He is so much into QA that even after work he provides training courses for junior QA engineers.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button