Friday, 28 August 2015

Spring REST JSON response

Spring REST JSON response

Spring’s @ResponseBody annotation and returning the object that we want to send to the client.

The @ResponseBody annotation tells Spring that we will be returning data in the response body rather than rendering a JSP.

When the @ResponseBody annotation is used, Spring will return the data in a format that is acceptable to the client.

JSON response:
If the client request has a header to accept JSON and Jackson-Mapper is present in the classpath, then Spring will try to serialize the return value to JSON.

XML response:
If the request header indicates XML as acceptable (accept= application/xml) and Java Architecture for XML Binding (JAXB) is in the classpath and the return type is annotated with JAXB annotation, Spring will try to marshall the return value to XML.

<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.9.12</version>
</dependency>

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.WebRequest;
import com.abusecore.controller.RestController;
import com.abusecore.model.Count;
import com.abusecore.services.IDataServices;

@Controller
@RequestMapping("/AbuseCore-1")
public class RestController {

       @Autowired
       IDataServices dataServices;
      
      
       /** Logger class to display logs. */
       static final Logger logger = Logger.getLogger(RestController.class);

      
       @RequestMapping(value="/count-tickets.json",method=RequestMethod.GET)
       public @ResponseBody Count getTicketsCount() {
              Count count = dataServices.getTicketsCount();
              logger.info("total tickets :" + count);
              return count;
       }
}

Singleton beans scopes in Spring

Beans with different Id’s have Singleton instance


singleton: (Default) Scopes a single bean definition to a single object instance per Spring IoC container.


NO, Similar beans with different ids make the different instance (Even scope is defined as Singleton); because the singleton scope checked on the basis of bean ids.

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

       default-autowire="byName" default-autowire-candidates="*">

       <context:annotation-config />

       <bean id="id1" class="spring.core.singleton.Singleton">
              <property name="name" value="Rajesh kumar"></property>
       </bean>

       <bean id="id2" class="spring.core.singleton.Singleton">
              <property name="name" value="Awadh kumar"></property>
       </bean>
</beans>

package spring.core.singleton;
public class Singleton {
      
       private String name;

       public String getName() {
              return name;
       }

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

package spring.core.singleton;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestSingletonScope {
       public static void main(String[] args) {

              ApplicationContext appContext = new ClassPathXmlApplicationContext
                           ("spring/core/singleton/autowiring-spring.xml");
             
              Singleton singleton1 = appContext.getBean("id1", Singleton.class);
             
              Singleton singleton2 = appContext.getBean("id2", Singleton.class);
             
             
              System.out.println(singleton1.getName());
              System.out.println(singleton2.getName());

              if(singleton1==singleton2) {
                     System.out.println("singleton");
              } else {
                     System.out.println("not singleton");
              }
       }
}

Output:
Rajesh kumar
Awadh kumar
not singleton
Related Posts Plugin for WordPress, Blogger...