Spring Interview Questions | Page 3


Question 21:

What is @RequestBody and why it is used in Spring?

   

Question 22:

What is @ResponseBody and why it is used in Spring?

If any method is annotated with @ResponseBody annotation in controller then Spring will bind the return value to outgoing HTTP response body which depends upon the Content-Type of HTTP request header.

Syntax of @ResponseBody

@Controller
public class ProductRestController 
{
    @RequestMapping(value = "/getProducts")
    public @ResponseBody List<ProductDto> getAllProducts() 
    {
        List<ProductDto> productDto = new ArrayList<ProductDto>();
        //code here 
        return productDto;
    }
}
Question 23:

What is view resolver in Spring MVC?

Controller returns an object of type ModelAndView. This object contains both the Model and the View. The model is a Map and the view can be a String view name which is resolved by a View Resolver which can be rendered.

List of view resolvers available in spring are-

AbstractCachingViewResolver, AbstractTemplateViewResolver, BeanNameViewResolver, ContentNegotiatingViewResolver, FreeMarkerViewResolver, GroovyMarkupViewResolver, InternalResourceViewResolver, JasperReportsViewResolver, ResourceBundleViewResolver, ScriptTemplateViewResolver, TilesViewResolver, TilesViewResolver, UrlBasedViewResolver, VelocityLayoutViewResolver, VelocityViewResolver, ViewResolverComposite, XmlViewResolver, XsltViewResolver

Question 24:

What is @RestController in Spring MVC?

Prior to Spring 4, you have to used @Controller and @ResponseBody to return domain object instead of view as shown below-

@Controller
public class ProductRestController 
{
    @RequestMapping(value = "/getProducts")
    public @ResponseBody List<ProductDto> getAllProducts() 
    {
        List<ProductDto> productDto = new ArrayList<ProductDto>();
        //code here 
        return productDto;
    }
}

In Spring 4, new annotation @RestController was introduced, which is used to mark the class as a controller where every method returns a domain object instead of a view.

It’s replacement for @Controller and @ResponseBody used together to return domain object as a response.

@RestController
public class ProductRestController 
{
    @RequestMapping(value = "/getProducts")
    public List<ProductDto> getAllProducts() 
    {
        List<ProductDto> productDto = new ArrayList<ProductDto>();
        //code here 
        return productDto;
    }
}
Question 25:

What is Spring Security?


Question 26:

Mention of the important annotations in annotation-based Spring configuration.

Share interview questions

Comments

Comments
comments powered by Disqus

Navigation

Social Media