Display Model As JSON or XML using Spring

For this sample, I already have an object named principles.Customer as follows:


@XmlRootElement(name = "customer", namespace = "http://www.w3.org/2001/XMLSchema-instance")
public class Customer implements Serializable {
 private String id;
 private String name;
 private List<Product> products;

public Customer() {}

public Customer(String id, String name) {
 this.id = id;
 this.name = name;
 this.products = new LinkedList<>();
 }

public String getId() {
 return id;
 }

public void setId(String id) {
 this.id = id;
 }

public String getName() {
 return name;
 }

public void setName(String name) {
 this.name = name;
 }

public List<Product> getProducts() {
 return products;
 }

public void setProducts(List<Product> products) {
 this.products = products;
 }

@Override
 public String toString() {
 return "Customer{" +
 "id='" + id + '\'' +
 ", name='" + name + '\'' +
 ", products=" + products +
 '}';
 }
}

Also, there is a StatusController as follows:


@Controller
public class StatusController {

@RequestMapping(value="/status", method= RequestMethod.GET)
 public @ResponseBody Customer getProduct(ModelMap model) {
 return new Customer(UUID.randomUUID().toString(), "Some Product Name Here");
 }
}

Finally, the configuration mvc-servlet.xml and web.xml files:

</pre>
&nbsp;

<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:p="http://www.springframework.org/schema/p"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:jms="http://www.springframework.org/schema/jms"
 xmlns:amq="http://activemq.apache.org/schema/core"
 xmlns:jee="http://www.springframework.org/schema/jee"
 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

 http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.2.xsd
http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd">

<context:component-scan base-package="mvc"/>
 <context:component-scan base-package="principles"/>
 <context:component-scan base-package="activemq"/>
 <context:component-scan base-package="akka"/>
 <context:component-scan base-package="commands"/>
 <context:component-scan base-package="events"/>
 <context:component-scan base-package="filter"/>
 <context:component-scan base-package="query"/>

<mvc:annotation-driven/>

<bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
 <property name="mediaTypes">
 <map>
 <entry key="json" value="application/json"/>
 <entry key="xml" value="application/xml"/>
 </map>
 </property>

<property name="defaultViews">
 <list>
 <bean id="xmlViewer"
 class="org.springframework.web.servlet.view.xml.MarshallingView">
 <constructor-arg>
 <bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
 <property name="classesToBeBound">
 <list>
 <value>principles.Customer</value>
 </list>
 </property>
 </bean>
 </constructor-arg>
 </bean>

<bean id="jsonViewer"
 class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
 <property name="objectMapper">
 <bean class="org.codehaus.jackson.map.ObjectMapper"/>
 </property>
 </bean>
 </list>
 </property>
 <property name="favorPathExtension" value="false" />
 <property name="favorParameter" value="false" />
 <property name="useNotAcceptableStatusCode" value="true" />
 </bean>
 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
 <property name="prefix">
 <value>/WEB-INF/jsp/</value>
 </property>
 <property name="suffix">
 <value>.jsp</value>
 </property>
 </bean>

<!-- active mq connection factory -->
 <bean id="amqConnectionFactory"
 class="org.apache.activemq.ActiveMQConnectionFactory"
 p:brokerURL="tcp://localhost:61616"
 p:userName="admin"
 p:password="admin"/>

<bean id="cachedConnectionFactory"
 class="org.springframework.jms.connection.CachingConnectionFactory"
 p:targetConnectionFactory-ref="amqConnectionFactory"
 p:sessionCacheSize="10"/>

<!-- queue destination -->
 <amq:queue id="principlesRequestQueue" physicalName="Principles.Request"/>
 <amq:queue id="principlesResponseQueue" physicalName="Principles.Response"/>

<!-- jms template instance -->
 <bean id="producerTemplate"
 class="org.springframework.jms.core.JmsTemplate"
 p:connectionFactory-ref="cachedConnectionFactory"
 p:defaultDestination-ref="principlesRequestQueue"/>

<!-- jms queue listener, response to requests -->
 <bean id="principlesListener" class="activemq.PrinciplesMessageConsumer"/>

<!-- jms listener container -->
 <jms:listener-container
 container-type="default"
 connection-factory="cachedConnectionFactory"
 acknowledge="auto"
 concurrency="10-50">
 <jms:listener destination="Principles.Request"
 response-destination="Principles.Response"
 ref="principlesListener"/>
 </jms:listener-container>
</beans>

&nbsp;

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<listener>
 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>

<context-param>
 <param-name>contextConfigLocation</param-name>
 <param-value>/WEB-INF/mvc-servlet.xml classpath*:/principles/applicationContext.xml</param-value>
 </context-param>

<servlet>
 <servlet-name>mvc</servlet-name>
 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 <load-on-startup>1</load-on-startup>
 </servlet>

<servlet-mapping>
 <servlet-name>mvc</servlet-name>
 <url-pattern>/</url-pattern>
 </servlet-mapping>
</web-app>
<pre>

 

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s