0

我正在尝试在控制器中使用@responseBody 来返回一个对象,我是否遇到异常。我在应用程序中有杰克逊罐子,它仍然说以下错误 ClassNotFoundException could not intiatiate

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

这是我的代码---->

 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());
        }
    }

这是jsp-->

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> (http://www.w3.org/TR/html4/loose.dtd%27%3E)
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<body>
<form:form method="post" action="addContact">

<table>
<tr>
    <td><form:label path="firstname">First Name</form:label></td>
    <td><form:input path="firstname" /></td>
</tr>
<tr>
    <td><form:label path="lastname">Last Name</form:label></td>
    <td><form:input path="lastname" /></td>
</tr>
<tr>
    <td><form:label path="lastname">Email</form:label></td>
    <td><form:input path="email" /></td>
</tr>
<tr>
    <td><form:label path="telephone">Telephone</form:label></td>
    <td><form:input path="telephone" /></td>
</tr>
<tr>
    <td colspan="2">
        <input type="submit" value="Add Contact"/>
    </td>
</tr>
</table> 

</form:form>
</body>
</html> 

这是servlet.xml--->

<?xml version="1.0" encoding="UTF-8"?>
<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"> (http://www.springframework.org/schema/context/spring-context-3.0.xsd%27%3E)

  <context:component-scan base-package="com.tcs.laks.sample" />

 <mvc:annotation-driven />
 <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
  <property name="mediaTypes">
    <map>
      <entry key="html" value="text/html"/>
      <entry key="json" value="application/json"/>
    </map>
  </property>
  <property name="viewResolvers">
    <list>
     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <!--  <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
-->        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
    </list>
  </property>
  <property name="defaultViews">
    <list>
      <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
        <property name="prefixJson" value="true"/>
      </bean>
    </list>
  </property>
</bean>   
</beans> 

这是豆子

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;
        }
} 

请告诉我为什么当 Spring 3 自动将对象转换为 json 时包含正确的 jar,但它无法实例化该类?

4

2 回答 2

3

在您的 servlet.xml 中包含 mvc:annotation-driven 应该可以工作。

如果您使用@responsebody,您将绕过视图解析器。AnnotationMethodHandlerAdapter 将实例化 MappingJacksonHttpMessageConverter 如果它存在于写入响应体的类路径中。

希望这也有效。

于 2013-02-10T08:13:54.157 回答
1

我自己找到了答案,主要问题是添加了 servlet.xml 文件

<bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/> 

做了这项工作。

于 2013-02-21T06:19:04.893 回答