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:
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"
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 ?
2) The InternalResourceViewResolver is also the default view resolver of DispatcherServlet class, which acts as the front controller in the Spring MVC framework.
Below, we will discuss about three important View Resolver implementations provided by Spring MVC, InternalResourceViewResolver , XmlViewResolver and ResourceBundleViewResolver .
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With