Note that there are some explanatory texts on larger screens.

plurals
  1. POReturning object using @ResponseBody with spring 3 doesn't work
    primarykey
    data
    text
    <p>I am trying to use @responseBody in the controller to return a object am i getting exception. I have jackson jar in the application still it says the following error ClassNotFoundException could not intiatiate </p> <pre><code>HTTP Status 500 - -------------------------------------------------------------------------------- type Exception report message description The server encountered an internal error () that prevented it from fulfilling this request. exception root cause org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#0': Cannot create inner bean '(inner bean)' of type [org.springframework.http.converter.json.MappingJacksonHttpMessageConverter] while setting bean property 'messageConverters' with key [6]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#8': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.http.converter.json.MappingJacksonHttpMessageConverter]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: Could not initialize class org.codehaus.jackson.map.ObjectMapper root cause org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#8': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.http.converter.json.MappingJacksonHttpMessageConverter]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: Could not initialize class org.codehaus.jackson.map.ObjectMapper </code></pre> <p>This is my code----></p> <pre><code> import org.springframework.stereotype.Controller; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.SessionAttributes; import org.springframework.web.servlet.ModelAndView; import com.lol.laks.sample.beans.*; @Controller @SessionAttributes public class ContactsController { @RequestMapping(value = "/addContact", method = RequestMethod.POST, produces="application/json") public @ResponseBody Contacts addContact(@ModelAttribute("contact") Contacts contact,BindingResult result) { System.out.println("First Name:" + contact.getFirstname() + "Last Name:" + contact.getLastname()); return contact; } @RequestMapping("/") public ModelAndView showContacts() { return new ModelAndView("contacts", "command", new Contacts()); } } </code></pre> <p>This is the jsp--></p> <pre><code>&lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt; (http://www.w3.org/TR/html4/loose.dtd%27%3E) &lt;html&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt; &lt;title&gt;Insert title here&lt;/title&gt; &lt;/head&gt; &lt;%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%&gt; &lt;body&gt; &lt;form:form method="post" action="addContact"&gt; &lt;table&gt; &lt;tr&gt; &lt;td&gt;&lt;form:label path="firstname"&gt;First Name&lt;/form:label&gt;&lt;/td&gt; &lt;td&gt;&lt;form:input path="firstname" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;form:label path="lastname"&gt;Last Name&lt;/form:label&gt;&lt;/td&gt; &lt;td&gt;&lt;form:input path="lastname" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;form:label path="lastname"&gt;Email&lt;/form:label&gt;&lt;/td&gt; &lt;td&gt;&lt;form:input path="email" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;form:label path="telephone"&gt;Telephone&lt;/form:label&gt;&lt;/td&gt; &lt;td&gt;&lt;form:input path="telephone" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td colspan="2"&gt; &lt;input type="submit" value="Add Contact"/&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/form:form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>This is the servlet.xml---></p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 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"&gt; (http://www.springframework.org/schema/context/spring-context-3.0.xsd%27%3E) &lt;context:component-scan base-package="com.tcs.laks.sample" /&gt; &lt;mvc:annotation-driven /&gt; &lt;bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"&gt; &lt;property name="mediaTypes"&gt; &lt;map&gt; &lt;entry key="html" value="text/html"/&gt; &lt;entry key="json" value="application/json"/&gt; &lt;/map&gt; &lt;/property&gt; &lt;property name="viewResolvers"&gt; &lt;list&gt; &lt;bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt; &lt;!-- &lt;property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /&gt; --&gt; &lt;property name="prefix" value="/WEB-INF/jsp/" /&gt; &lt;property name="suffix" value=".jsp" /&gt; &lt;/bean&gt; &lt;/list&gt; &lt;/property&gt; &lt;property name="defaultViews"&gt; &lt;list&gt; &lt;bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"&gt; &lt;property name="prefixJson" value="true"/&gt; &lt;/bean&gt; &lt;/list&gt; &lt;/property&gt; &lt;/bean&gt; &lt;/beans&gt; </code></pre> <p>This is the beans</p> <pre><code>import org.springframework.stereotype.Component; @Component public class Contacts { private String firstname; private String lastname; private String email; private String telephone; public String getFirstname() { return firstname; } public void setFirstname(String firstname) { this.firstname = firstname; } public String getLastname() { return lastname; } public void setLastname(String lastname) { this.lastname = lastname; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getTelephone() { return telephone; } public void setTelephone(String telephone) { this.telephone = telephone; } } </code></pre> <p>Please Tell me why it is not able to instantiate the class though the proper jars are included when Spring 3 automatically converts the object to json?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload