Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC but without a view resolver?

Tags:

spring

is it possible to use Spring 3.1 MVC without using a view resolver?

The reason why i ask is because i simply want to build and create a Web service, not a Website so i do not need to render any JSP or html pages at all. I want to build a RESTful Web service using Spring 3.1.

Is this possible?

this is how my servlett looks like which is taken from a tutorial:

Here is my mvc-config.xml Here is my Web.xml
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
         http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

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

    <servlet-mapping>
        <servlet-name>FreedomSpring</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>


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

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>

        </param-value>
    </context-param>

    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>/WEB-INF/log4j.xml</param-value>
    </context-param>

    <welcome-file-list>
        <welcome-file>
      index.jsp
    </welcome-file>
    </welcome-file-list>

    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>

</web-app>

And here is my controller java class that simply want to return a String from a specific http REST request and not a "ModelAndView" object so to speak

  package com.jr.freedom.controllers;

    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;

    @Controller
    public class Hello {

        @RequestMapping(value = "/hello", method = RequestMethod.GET)
        public String helloWorldInJson() {

            return "hello";
        }
    }

Also, How can i capture a requests Parameters that a client may send me? is that possible using springs annototations? i know that i can in previous Spring 2.x use HttpServletRequest and HttpServletResponse to get any parameters sent from a client and also return a object back to the user as well such as a JSON object.

i am just looking for a simple example that does this:

  1. Get parameters and possibly HTTP headers from a clients Request on my controller class and map them to some Java object "example, a client sends me details of a new user object registering to my backend service(username, password etc etc) and i want to be able to map this to my Java class Called User"

  2. Return a response of any kind of object such as a String, json or xml data to a client.

Im quite new to Spring 3.0. i did a bit of work on Spring 2.0 long ago but annotations seems the way to go now and do not know how to do it via annotations.

Thanks

Also, To execute the above controller method of helloWorldInJson() do i simply call http://localhost/FreedomSpring/hello ?

like image 633
Jonathan Avatar asked Feb 10 '12 15:02

Jonathan


People also ask

What is the default view resolver in Spring MVC?

2) The InternalResourceViewResolver is also the default view resolver of DispatcherServlet class, which acts as the front controller in the Spring MVC framework.

What are the different types of ViewResolver in Spring MVC?

Below, we will discuss about three important View Resolver implementations provided by Spring MVC, InternalResourceViewResolver , XmlViewResolver and ResourceBundleViewResolver .

What is the role of view resolver in Spring MVC?

Spring MVC is a Web MVC Framework for building web applications. In generic all MVC frameworks provide a way of working with views. Spring does that via the ViewResolvers, which enables you to render models in the browser without tying the implementation to specific view technology.

Is abstract view resolver is a type of view resolver?

Abstract view resolver that caches views. Often views need preparation before they can be used; extending this view resolver provides caching. Implementation of ViewResolver that accepts a configuration file written in XML with the same DTD as Spring's XML bean factories.


1 Answers

You generally use @ResponseBody for this.

See 16.3.3.5 Mapping the response body with the @ResponseBody annotation.

This bypasses the view resolver stuff altogether.

like image 153
skaffman Avatar answered Oct 12 '22 12:10

skaffman