Monday 13 January 2014

ContentNagotiationViewResolver example using Spring MVC

ContentNagotiationViewResolver  using Spring MVC

Hello Friends.hope you are doing well.today i am going to demonstrate you ContentNagotiationViewResolver .suppose i want to access the same resource with differant media types.than client can access the resource using ContentNagotiationViewResolver.suppose client want xml form of resouce than he can get it using ContentNagotiationViewResolver  feature of spring mvc.

Spring oxm and jackson Api should be on classpath for execution of above code:

Sample code i am going to demonstrate is given below:

Model Class (jaxb annotated)


package org.demo.model;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Student
{
   
    int id;
   
    String name;
   

    public Student() { }
    public Student(int id,String name)
    {
        this.id=id;
        this.name=name;
    }
    public int getId() {
        return id;
    }
   
    @XmlElement
    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }
    @XmlElement
    public void setName(String name) {
        this.name = name;
    }


}

StudentController.java

package org.demo.controller;

import org.demo.model.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value="/Student")
public class StudentController
{

@RequestMapping(value="{id}/{name}",method=RequestMethod.GET,produces="application/json")
public String getStudent(@PathVariable int id,@PathVariable String name,ModelMap model)
{
        Student s=new Student(id,name);
    model.addAttribute("student", s);
       
    return "Data";
   
}
}
 

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>SpringContentNagotioationViewResolver</display-name>
  <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
  <servlet-name>ContentNagotiation</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  <servlet-name>ContentNagotiation</servlet-name>
      <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

ContentNagotiation-servlet.xml


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans    
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">


 <context:component-scan base-package="org.demo.controller"></context:component-scan>
 <mvc:annotation-driven/>
 <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
 <property name="order" value="1"></property>

 <property name="mediaTypes">
 <map>
         <entry key="json" value="application/json"></entry>
         <entry key="xml" value="application/xml"></entry>
   </map>
 </property>

 <property name="defaultViews">
        <list>
          <!-- JSON View -->
          <bean
            class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
          </bean>

       

          <!-- JAXB XML View -->
          <bean class="org.springframework.web.servlet.view.xml.MarshallingView">
            <constructor-arg>
                <bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
                   <property name="classesToBeBound">
                    <list>
                       <value>org.demo.model.Student</value>
                    </list>
                   </property>
                </bean>
            </constructor-arg>
          </bean>
         </list>
      </property>
      <property name="ignoreAcceptHeader" value="true" />

    </bean>

   
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="order" value="2" />
        <property name="prefix">
            <value>/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

 </beans>


Data.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD  HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

Student Id: ${student.id}
Student Name: ${student.name}
</body>
</html>

The output of the code is look like below:








i hope you will get my point.for any query ping me on pathak.nisarg@yahoo.com.
Thanks for reading this article.
Regards.

No comments:

Post a Comment

Spring Boot SSL configuration -Tomcat Server

Hi Friends hope you all are doing well. Today I am going to demonstrate about how to configure SSL in Spring boot web Application. Need o...