简体   繁体   English

java.lang.NoSuchMethodException:[Lcom.dn.vo.User;。 <init> ()

[英]java.lang.NoSuchMethodException: [Lcom.dn.vo.User;.<init>()

I am using Spring MVC to build a web application. 我正在使用Spring MVC构建Web应用程序。 I created a new VO named User and created a POST method to process the VO. 我创建了一个名为User的新VO,并创建了一个POST方法来处理VO。 The VO is having default constructor, yet I am getting VO具有默认构造函数,但我正在

java.lang.NoSuchMethodException: [Lcom.dn.vo.User;.<init>(). 

The code involved is as follows: 涉及的代码如下:

User VO 用户VO

package com.dn.vo;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

import org.springframework.beans.factory.annotation.Autowired;

import com.dn.enums.Salutation;


@Entity
@Table(name = "PHONE_USER")
public class User implements Serializable {

    long userId;
    String firstName;
    String surName;
    Salutation salutation; 

    public User() {
        super();
    }

    public User(long userId, String firstName, String surName, Salutation salutation) {
        super();
        this.userId = userId;
        this.firstName = firstName;
        this.surName = surName;
        this.salutation = salutation;
    }

    @Id
    @GeneratedValue
    @Column(name = "USER_ID", unique = true, nullable = false, precision = 5, scale = 0)
    public long getUserId() {
        return userId;
    }

    public void setUserId(long userId) {
        this.userId = userId;
    }

    @Column(name = "FIRST_NAME", nullable = false, length = 20)
    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }


    @Column(name = "SURNAME", nullable = false, length = 20)
    public String getSurName() {
        return surName;
    }

    public void setSurName(String surName) {
        this.surName = surName;
    }

    @Column(name = "SALUTATION", nullable = false, length = 20)
    public Salutation getSalutation() {
        return salutation;
    }

    public void setSalutation(Salutation salutation) {
        this.salutation = salutation;
    }

    @Override
    public String toString() {
      return firstName+" "+surName;
    }

}

MVC Controller MVC控制器

package com.dn.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dn.db.DBConnectionProvider;
import com.dn.vo.User;

@Controller
public class UserRegistrationController { 

    @Autowired
    private DBConnectionProvider dbConnectionProvider; 


    @ResponseBody @RequestMapping(value= "/createUser", method = RequestMethod.POST, headers="Accept=application/json")
    public void createUser( User user) {

        System.out.println(user.toString());

    }



@ResponseBody @RequestMapping(value= "/createUsers", method = RequestMethod.POST, headers="Accept=application/json")
    public void createUser( User[] user) {

        System.out.println(user.toString());

    }


}

HTTP Client Test HTTP客户端测试

package com.dn.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.codehaus.jackson.map.ObjectMapper;

import com.dn.enums.Salutation;
import com.dn.vo.User;

public class UserRegistrationControllerTest  {

    public static void main(String[] args) {

        UserRegistrationControllerTest test = new UserRegistrationControllerTest();
        test.testUserArrayInput();
    }

    private void testSingleUserInput(){

        HttpClient httpClient = null;
        try {
            httpClient = new DefaultHttpClient();
            String url = "http://localhost:8080/DNServer/createUser";
            HttpPost httpPost = new HttpPost(url);
            httpPost.setHeader(new BasicHeader("Accept", "application/json"));

            User region = new User();
            region.setFirstName("ABC");
            region.setSurName("XYZ");
            region.setSalutation(Salutation.Mr);

            ObjectMapper mapper = new ObjectMapper();
            System.out.println( mapper.writeValueAsString(region));

            List nameValuePairs = new ArrayList();
            nameValuePairs.add(new BasicNameValuePair("user",  mapper.writeValueAsString(region)));
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            HttpResponse response = httpClient.execute(httpPost);

            System.out.println("test Single User Input done"+response);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    private void testUserArrayInput(){
        HttpClient httpClient = null;
        try {
            httpClient = new DefaultHttpClient();
            String url = "http://localhost:8080/DNServer/createUsers";
            HttpPost httpPost = new HttpPost(url);
            httpPost.setHeader(new BasicHeader("Accept", "application/json"));

            User[] regions = new User[1];
            User region = new User();
            region.setFirstName("XYZ");
            region.setSurName("ABC");
            region.setSalutation(Salutation.Mr);
            regions[0] = region;



            ObjectMapper mapper = new ObjectMapper();
            System.out.println( mapper.writeValueAsString(regions));

            List nameValuePairs = new ArrayList();
            nameValuePairs.add(new BasicNameValuePair("users",  mapper.writeValueAsString(regions)));
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            HttpResponse response = httpClient.execute(httpPost);
            System.out.println("done");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}

Full Stack Trace 全栈跟踪

SEVERE: Servlet.service() for servlet [DNServlet] in context with path [/DNServer] threw exception [Request processing failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [[Lcom.dn.vo.User;]: No default constructor found; nested exception is java.lang.NoSuchMethodException: [Lcom.dn.vo.User;.<init>()] with root cause
java.lang.NoSuchMethodException: [Lcom.dn.vo.User;.<init>()
    at java.lang.Class.getConstructor0(Class.java:2721)
    at java.lang.Class.getDeclaredConstructor(Class.java:2002)
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:105)
    at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.createAttribute(ModelAttributeMethodProcessor.java:138)
    at org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor.createAttribute(ServletModelAttributeMethodProcessor.java:81)
    at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:104)
    at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:79)
    at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:157)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:124)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:644)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:301)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:136)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:74)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:526)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1017)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:652)
    at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1575)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1533)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)`enter code here`

I think that the clue is in the method name: 我认为线索在方法名称中:

  [Lcom.dn.vo.User;.<init>()

When we deconstruct that, Spring appears to be trying to invoke a no-args constructor (the <init>() "method") on the type [Lcom.dn.vo.User; 当我们对它进行解构时,Spring似乎正在尝试对类型[Lcom.dn.vo.User;的类型调用no-args构造函数( <init>() “方法”) [Lcom.dn.vo.User; . But that type means "array of com.dn.vo.User ". 但是该类型表示“ com.dn.vo.User数组”。 And array classes don't have constructors. 而且数组类没有构造函数。

I can see some code in your unit test that creates a User[] and appears to pass it as an argment in a POST request to "/createUsers". 我可以在您的单元测试中看到一些代码,这些代码创建了User[]并似乎将它作为argment在POST请求中传递给“ / createUsers”。 But the MVC class doesn't have a method that binds to "/createUsers". 但是MVC类没有绑定到“ / createUsers”的方法。

Is that right??? 那正确吗???

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.NoSuchMethodException: <init> - java.lang.NoSuchMethodException: <init> java.lang.NoSuchMethodException:userAuth.User。<init> ()</init> - java.lang.NoSuchMethodException: userAuth.User.<init>() java.lang.NoSuchMethodException:AffirmativeBased。 <init> () - java.lang.NoSuchMethodException: AffirmativeBased.<init>() Kotlin java.lang.NoSuchMethodException:<init> ()</init> - Kotlin java.lang.NoSuchMethodException: <init>() java.lang.NoSuchMethodException - java.lang.NoSuchMethodException java.lang.NoSuchMethodException:package.ClassName。<init> () 使用 Class<!--?--></init> - java.lang.NoSuchMethodException : package.ClassName.<init>() Using Class<?> Java - java.lang.NoSuchMethodException - Java - java.lang.NoSuchMethodException 摘要解析器错误java.lang.NoSuchMethodException:员工。 <init> () - digester parser error java.lang.NoSuchMethodException: Employee.<init>() 与SAAgent(三星附件)java.lang.NoSuchMethodException的Proguard问题: <init> [] - Proguard issue with SAAgent(Samsung Accessory) java.lang.NoSuchMethodException: <init> [] Hadoop错误中的自定义分区错误java.lang.NoSuchMethodException:- <init> () - custom partitioner in Hadoop error java.lang.NoSuchMethodException:- <init>()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM