我是Angular 4的初学者以及弹簧靴休息。
我做了一个简单的应用程序,其中Angular 4作为前端,Spring Rest作为API。我试图从Angular http请求中调用Spring Rest控制器方法(Post)。
以下是Angular 4代码。
import { Injectable } from '@angular/core';
import { Http, Headers, Response, RequestOptions } from '@angular/http';
@Injectable()
export class ServerService {
constructor(private http: Http) { }
headers = new Headers({"Content-Type": "application/json"});
storeServers(students: any[]) {
const WEBSERVICE_ADDRESS_STUDENT: string = "http://localhost:8080/api/saveStudent";
return this.http.post(WEBSERVICE_ADDRESS_STUDENT,JSON.stringify(students),{ headers: this.headers })
.subscribe(
(response) => console.log(response),
(error) => console.log("error: ", + error)
);
}
}
/************* Following is the student Array *******************/
student = [
{
StudentID: 102,
Name: 'abc',
Surname: 'xyz',
City: 'Space'
},
{
StudentID: 103,
Name: 'jack',
Surname: 'th',
City: 'Ocean'
}
];
以下是我的Spring Rest API:
@RestController
@RequestMapping("api")
public class StudentDetailsController {
private StudentRepository studentRepositor;
@Autowired
public StudentDetailsController(StudentRepository theStudentDetailDAO)
{
this.studentRepositor = theStudentDetailDAO;
}
@RequestMapping(value="/saveStudent", method = RequestMethod.POST,produces = "application/json")
public void saveStudent(StudentDetails[] objStudentDetails)
{
List<StudentDetails> lstStudentDetais = null;
lstStudentDetais = studentRepositor.findAll();
studentRepositor.save(objStudentDetails[0]);
}
}
当我从Angular App点击服务时,它会在我的Java应用程序中抛出以下错误:
java.lang.NoSuchMethodException: [Lcom.springboot.cruddemo.entity.StudentDetails;.<init>()
at java.lang.Class.getConstructor0(Class.java:3082) ~[na:1.8.0_202]
at java.lang.Class.getDeclaredConstructor(Class.java:2178) ~[na:1.8.0_202]
at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.createAttribute(ModelAttributeMethodProcessor.java:216) ~[spring-web-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor.createAttribute(ServletModelAttributeMethodProcessor.java:84) ~[spring-webmvc-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:139) ~[spring-web-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:126) ~[spring-web-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:166) ~[spring-web-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:134) ~[spring-web-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102) ~[spring-webmvc-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895) ~[spring-webmvc-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:800) ~[spring-webmvc-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1038) ~[spring-webmvc-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942) ~[spring-webmvc-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005) ~[spring-webmvc-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:908) ~[spring-webmvc-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:660) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882) ~[spring-webmvc-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.16.jar:9.0.16]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
at com.luv2code.springboot.cruddemo.rest.CORSFilter.doFilter(CORSFilter.java:31) ~[classes/:na]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) ~[spring-web-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92) ~[spring-web-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93) ~[spring-web-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200) ~[spring-web-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:200) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-9.0.16.jar:9.0.16]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490) [tomcat-embed-core-9.0.16.jar:9.0.16]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) [tomcat-embed-core-9.0.16.jar:9.0.16]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) [tomcat-embed-core-9.0.16.jar:9.0.16]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) [tomcat-embed-core-9.0.16.jar:9.0.16]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) [tomcat-embed-core-9.0.16.jar:9.0.16]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408) [tomcat-embed-core-9.0.16.jar:9.0.16]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-9.0.16.jar:9.0.16]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:834) [tomcat-embed-core-9.0.16.jar:9.0.16]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1415) [tomcat-embed-core-9.0.16.jar:9.0.16]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.16.jar:9.0.16]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_202]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_202]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.16.jar:9.0.16]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_202]
以下是我的StudentDetails类,我在那里定义了所有必需的构造函数:
package com.luv2code.springboot.cruddemo.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "StudentDetails")
public class StudentDetails {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="StudentID")
private Integer StudentID;
@Column(name="Name")
private String Name;
@Column(name="Surname")
private String Surname;
@Column(name="City")
private String City;
public StudentDetails() {}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getSurname() {
return Surname;
}
public void setSurname(String surname) {
Surname = surname;
}
public String getCity() {
return City;
}
public void setCity(String city) {
City = city;
}
public StudentDetails(String name, String surname, String city) {
Name = name;
Surname = surname;
City = city;
}
@Override
public String toString() {
return "StudentDetails [Name=" + Name + ", Surname=" + Surname + ", City=" + City + "]";
}
}
我不明白哪个构造函数错误是指:(
以下是chrome开发人员工具栏说的内容:
error: "Internal Server Error"
message: "No primary or default constructor found for class [Lcom.luv2code.springboot.cruddemo.entity.StudentDetails;"
path: "/api/saveStudent"
status: 500
timestamp: "2019-03-30T06:38:17.757+0000"
trace: "java.lang.IllegalStateException: No primary or default constructor found for class [Lcom.luv2code.springboot.cruddemo.entity.StudentDetails;
↵ at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.createAttribute(ModelAttributeMethodProcessor.java:219)
↵ at org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor.createAttribute(ServletModelAttributeMethodProcessor.java:84)
↵ at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:139)
↵ at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:126)
↵ at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:166)
↵ at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:134)
↵ at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)
↵ at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)
↵ at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:800)
↵ at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
↵ at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1038)
↵ at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)
↵ at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)
↵ at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:908)
↵ at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
↵ at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)
↵ at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
↵ at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
↵ at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
↵ at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
↵ at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
↵ at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
↵ at com.luv2code.springboot.cruddemo.rest.CORSFilter.doFilter(CORSFilter.java:31)
↵ at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
↵ at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
↵ at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
↵ at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
↵ at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
↵ at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
↵ at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92)
↵ at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
↵ at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
↵ at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
↵ at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93)
↵ at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
↵ at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
↵ at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
↵ at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)
↵ at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
↵ at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
↵ at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
↵ at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:200)
↵ at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
↵ at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
↵ at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
↵ at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
↵ at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
↵ at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
↵ at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
↵ at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
↵ at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:834)
↵ at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1415)
↵ at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
↵ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
↵ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
↵ at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
↵ at java.lang.Thread.run(Thread.java:748)
↵Caused by: java.lang.NoSuchMethodException: [Lcom.luv2code.springboot.cruddemo.entity.StudentDetails;.<init>()
↵ at java.lang.Class.getConstructor0(Class.java:3082)
↵ at java.lang.Class.getDeclaredConstructor(Class.java:2178)
↵ at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.createAttribute(ModelAttributeMethodProcessor.java:216)
↵ ... 56 more
↵"
遵循chrome开发人员工具栏关于整个事情的说法:
error: "Internal Server Error"
message: "No primary or default constructor found for class [Lcom.luv2code.springboot.cruddemo.entity.StudentDetails;"
path: "/api/saveStudent"
status: 500
timestamp: "2019-03-30T06:38:17.757+0000"
trace: "java.lang.IllegalStateException: No primary or default constructor found for class [Lcom.luv2code.springboot.cruddemo.entity.StudentDetails;
↵ at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.createAttribute(ModelAttributeMethodProcessor.java:219)
↵ at org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor.createAttribute(ServletModelAttributeMethodProcessor.java:84)
↵ at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:139)
↵ at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:126)
↵ at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:166)
↵ at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:134)
↵ at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)
↵ at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)
↵ at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:800)
↵ at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
↵ at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1038)
↵ at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)
↵ at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)
↵ at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:908)
↵ at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
↵ at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)
↵ at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
↵ at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
↵ at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
↵ at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
↵ at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
↵ at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
↵ at com.luv2code.springboot.cruddemo.rest.CORSFilter.doFilter(CORSFilter.java:31)
↵ at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
↵ at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
↵ at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
↵ at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
↵ at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
↵ at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
↵ at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92)
↵ at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
↵ at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
↵ at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
↵ at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93)
↵ at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
↵ at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
↵ at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
↵ at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)
↵ at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
↵ at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
↵ at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
↵ at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:200)
↵ at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
↵ at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
↵ at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
↵ at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
↵ at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
↵ at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
↵ at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
↵ at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
↵ at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:834)
↵ at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1415)
↵ at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
↵ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
↵ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
↵ at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
↵ at java.lang.Thread.run(Thread.java:748)
↵Caused by: java.lang.NoSuchMethodException: [Lcom.luv2code.springboot.cruddemo.entity.StudentDetails;.<init>()
↵ at java.lang.Class.getConstructor0(Class.java:3082)
↵ at java.lang.Class.getDeclaredConstructor(Class.java:2178)
↵ at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.createAttribute(ModelAttributeMethodProcessor.java:216)
↵ ... 56 more
↵"
以下是数组中的null元素:
我通过做一些小改动成功执行了上面的代码。
调节器
@RequestMapping(value = "/saveStudent", method = RequestMethod.POST, produces = "application/json")
public void saveStudent(@RequestBody StudentDetails[] objStudentDetailsList) {
System.out.println(objStudentDetailsList[0].getName());
}
实体
@Entity
@Table(name = "StudentDetails")
public class StudentDetails {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long studentId;
private String name;
private String surName;
private String city;
public Long getStudentId() {
return studentId;
}
public void setStudentId(Long studentId) {
this.studentId = studentId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurName() {
return surName;
}
public void setSurName(String surName) {
this.surName = surName;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}