gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 06:30:02 26 4
gpt4 key购买 nike

我正在使用 Spring MVC 构建 Web 应用程序。我创建了一个名为 User 的新 VO,并创建了一个 POST 方法来处理该 VO。 VO 有默认构造函数,但我得到了

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

涉及的代码如下:

用户旁白

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

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 客户端测试

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


}

完整堆栈跟踪

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`

最佳答案

我认为线索就在方法名称中:

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

当我们解构它时,Spring 似乎试图在类型 <init>() 上调用无参数构造函数([Lcom.dn.vo.User;“方法”)。 。但该类型意味着“com.dn.vo.User 的数组”。并且数组类没有构造函数。

我可以在您的单元测试中看到一些创建 User[] 的代码并似乎将其作为 POST 请求中的参数传递给“/createUsers”。但是 MVC 类没有绑定(bind)到“/createUsers”的方法。

是吗???

关于java.lang.NoSuchMethodException : [Lcom. dn.vo.User;.<init>(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26233995/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com