Я работаю над Spring Boot версии 2, используя Java 8, и когда я пытаюсь добавить новый объект List в предопределенный объект List (т.е. topicList) в «createTopi c» метод Я получаю сообщение об ошибке.
**
TopicService. java
**
public class TopicService {
private List<TopicObject> topicList = Arrays.asList(
new TopicObject("Java", "Java Course", "Spring boot in java"),
new TopicObject("Maths", "Mathematics class","12th maths")
);
public String createTopic(TopicObject topicObj){
topicList.add(new TopicObject("Java1", "Java Course1", "Spring boot in java1"));
return "Request successful" ;
}
}
**
Тема объекта. java
**
package io.javabrains.springBootStarter.controller;
import com.fasterxml.jackson.annotation.JsonProperty;
public class TopicObject {
@JsonProperty("id")
private String id;
@JsonProperty("name")
private String name;
@JsonProperty("description")
private String description;
public TopicObject() {
}
public TopicObject(String id, String name, String description) {
this.id = id;
this.name = name;
this.description = description;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
**
Ошибка
**
2020-04-15 19: 52: 27.172 ОШИБКА 15312 --- [nio-8080-exe c -2] oa c. c. C. [. [. [/]. [DispatcherServlet]: Servlet.service () для сервлета [dispatcherServlet] в контексте с путем [] вызвало исключение [Ошибка обработки запроса; вложенное исключение - java .lang.UnsupportedOperationException] с root причиной
java .lang.UnsupportedOperationException: пусто в java .util.AbstractList.add (AbstractList. java: 148) ~ [na: 1.8.0_221] в java .util.AbstractList.add (AbstractList. java: 108) ~ [na: 1.8.0_221] в io.javabrains.springBootStarter.Service.TopicService.createTopi c (TopicService. java: 29) ~ [classes /: na] на io.javabrains.springBootStarter.controller.TopicController.createTopi c (TopicController. java: 31) ~ [классы /: на] на солнце. refle.NativeMethodAccessorImpl.invoke0 (Собственный метод) ~ [na: 1.8.0_221] в sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl. java: 62) ~ [na: 1.8.0_221] в sun.remokel. (DelegatingMethodAccessorImpl. java: 43) ~ [na: 1.8.0_221] в java .lang.reflect.Method.invoke (Method. java: 498) ~ [na: 1.8.0_221] в org.springframework .web.method.support.InvocableHandlerMethod.doInvoke (InvocableHandlerMethod. java: 209) ~ [spring-web-5 .0.13.RELEASE.jar: 5.0.13.RELEASE] at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest (InvocableHandlerMethod. java: 136) ~ [spring-web-5.0.13.RELEASE.jar 5.0.13.RELEASE] at org.springframework.web.servlet. mvc .method.annotation.ServletInvocableHandlerMethod.invokeAndHandle (ServletInvocableHandlerMethod. java: 102) ~ [spring-web mvcEASE. jar: 5.0.13.RELEASE] at org.springframework.web.servlet. mvc .method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod (RequestMappingHandlerAdapter. java: 894) ~ [spring-web mvc -5. RELEASE.jar: 5.0.13.RELEASE] в org.springframework.web.servlet. mvc .method.annotation.RequestMappingHandlerAdapter.handleInternal (RequestMappingHandlerAdapter. java: 800) ~ [spring-web mvc -5.0. 13.RELEASE.jar: 5.0.13.RELEASE] at org.springframework.web.servlet. mvc .method.AbstractHandlerMethodAdapter.handle (AbstractHandlerMethodAdapter. java: 87) ~ [spring-web mvc -5.0. 13.RELEASE.jar: 5.0.13.RELEASE] на org.springfram ework.web.servlet.DispatcherServlet.doDispatch (DispatcherServlet. java: 991) ~ [spring-web mvc -5.0.13.RELEASE.jar: 5.0.13.RELEASE] в org.springframework.web.servlet. DispatcherServlet.doService (DispatcherServlet. java: 925) ~ [spring-web mvc -5.0.13.RELEASE.jar: 5.0.13.RELEASE] в org.springframework.web.servlet.FrameworkServlet.processRequest (FrameworkServlet. java: 981) ~ [spring-web mvc -5.0.13.RELEASE.jar: 5.0.13.RELEASE] по адресу org.springframework.web.servlet.FrameworkServlet.doPost (FrameworkServlet. java: 884) ~ [spring-web mvc -5.0.13.RELEASE.jar: 5.0.13.RELEASE] по адресу javax.servlet.http.HttpServlet.service (HttpServlet. java: 661) ~ [tomcat-embed-core- 8.5.39.jar: 8.5.39] at org.springframework.web.servlet.FrameworkServlet.service (FrameworkServlet. java: 858) ~ [spring-web mvc -5.0.13.RELEASE.jar: 5.0. 13.RELEASE] на javax.servlet.http.HttpServlet.service (HttpServlet. java: 742) ~ [tomcat-embed-core-8.5.39.jar: 8.5.39] на орг. apache .catalina. core.ApplicationFilterChain.internalDoFilter (Applicati onFilterChain. java: 231) ~ [tomcat-embed-core-8.5.39.jar: 8.5.39] на орг. apache .catalina.core. ApplicationFilterChain.doFilter (ApplicationFilterChain. java: 166) ~ [tomcat-embed-core-8.5.39.jar: 8.5.39] в орг. apache .tomcat.websocket.server.WsFilter.doFilter (WsFilter. java: 52) ~ [tomcat-embed-websocket-8.5.39.jar: 8.5.39] в орг. apache .catalina.core.ApplicationFilterChain.internalDoFilter (ApplicationFilterChain. java: 193) ~ [tomcat- embed-core-8.5.39.jar: 8.5.39] в орг. apache .catalina.core.ApplicationFilterChain.doFilter (ApplicationFilterChain. java: 166) ~ [tomcat-embed-core-8.5.39.jar : 8.5.39] at org.springframework.web.filter.RequestContextFilter.doFilterInternal (RequestContextFilter. java: 99) ~ [spring-web-5.0.13.RELEASE.jar: 5.0.13.RELEASE] в org.springframework .web.filter.OncePerRequestFilter.doFilter (OncePerRequestFilter. java: 107) ~ [spring-web-5.0.13.RELEASE.jar: 5.0.13.RELEASE] в орг. apache .catalina.core.ApplicationFilterChain. internalDoFilter (ApplicationFilterChain. java: 193) ~ [tomcat-embed-core-8.5.39.jar: 8.5.39] в орг. apache .catalina.core.ApplicationFilterChain.doFilter (ApplicationFilterChain. java: 166) ~ [tomcat-embed-core-8.5.39.jar: 8.5.39] в org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal (HttpPutFormContentFilter. java: 109 ~ [ spring-web-5.0.13.RELEASE.jar: 5.0.13.RELEASE] по адресу org.springframework.web.filter.OncePerRequestFilter.doFilter (OncePerRequestFilter. java: 107) ~ [spring-web-5.0.13.RELEASE .jar: 5.0.13.RELEASE] at org. apache .catalina.core.ApplicationFilterChain.internalDoFilter (ApplicationFilterChain. java: 193) ~ [tomcat-embed-core-8.5.39.jar: 8.5.39] в орг. apache .catalina.core.ApplicationFilterChain.doFilter (ApplicationFilterChain. java: 166) ~ [tomcat-embed-core-8.5.39.jar: 8.5.39] в org.springframework.web.filter. HiddenHttpMethodFilter.doFilterInternal (HiddenHttpMethodFilter. java: 93) ~ [spring-web-5.0.13.RELEASE.jar: 5.0.13. : 107) ~ [spring-web-5.0.13.RELEASE.jar: 5.0.13.RELEASE] в орг. apache .catalina.core.ApplicationFil terChain.internalDoFilter (ApplicationFilterChain. java: 193) ~ [tomcat-embed-core-8.5.39.jar: 8.5.39] в орг. apache .catalina.core.ApplicationFilterChain.doFilter (ApplicationFilterChain. java) : 166) ~ [tomcat-embed-core-8.5.39.jar: 8.5.39] в org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal (CharacterEncodingFilter. java: 200) ~ [spring-web-5.0. 13.RELEASE.jar: 5.0.13.RELEASE] at org.springframework.web.filter.OncePerRequestFilter.doFilter (OncePerRequestFilter. java: 107) ~ [spring-web-5.0.13.RELEASE.jar: 5.0.13 .RELEASE] at org. apache .catalina.core.ApplicationFilterChain.internalDoFilter (ApplicationFilterChain. java: 193) ~ [tomcat-embed-core-8.5.39.jar: 8.5.39] в org. apache .catalina.core.ApplicationFilterChain.doFilter (ApplicationFilterChain. java: 166) ~ [tomcat-embed-core-8.5.39.jar: 8.5.39] в орг. apache .catalina.core.StandardWrapperValve.invoke ( StandardWrapperValve. java: 199) ~ [tomcat-embed-core-8.5.39.jar: 8.5.39] в орг. apache .catalina.core.StandardContextValve.invoke (St andardContextValve. java: 96) [tomcat-embed-core-8.5.39.jar: 8.5.39] в орг. apache .catalina.authenticator.AuthenticatorBase.invoke (AuthenticatorBase. java: 493) [tomcat -embed-core-8.5.39.jar: 8.5.39] в орг. apache .catalina.core.StandardHostValve.invoke (StandardHostValve. java: 137) [tomcat-embed-core-8.5.39.jar : 8.5.39] в орг. apache .catalina.valves.ErrorReportValve.invoke (ErrorReportValve. java: 81) [tomcat-embed-core-8.5.39.jar: 8.5.39] в орг. apache .catalina.core.StandardEngineValve.invoke (StandardEngineValve. java: 87) [tomcat-embed-core-8.5.39.jar: 8.5.39] в орг. apache .catalina.connector.CoyoteAdapter.service ( CoyoteAdapter. java: 343) [tomcat-embed-core-8.5.39.jar: 8.5.39] в орг. apache .coyote.http11.Http11Processor.service (Http11Processor. java: 798) [tomcat -embed-core-8.5.39.jar: 8.5.39] в org. apache .coyote.AbstractProcessorLight.process (AbstractProcessorLight. java: 66) [tomcat-embed-core-8.5.39.jar: 8.5.39] в org. apache .coyote.AbstractProtocol $ ConnectionHandler. process (AbstractProtocol. java: 806) [tomcat-embed-core-8.5.39.jar: 8.5.39] в орг. apache .tomcat.util. net .NioEndpoint $ SocketProcessor.doRun (NioEndpoint. java: 1498) [tomcat-embed-core-8.5.39.jar: 8.5.39] в орг. apache .tomcat.util. net .SocketProcessorBase.run (SocketProcessorBase. java: 49) [tomcat-embed-core-8.5.39.jar: 8.5.39] в java .util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor. java: 1149) [na: 1.8.0_221] в java. util.concurrent.ThreadPoolExecutor $ Worker.run (ThreadPoolExecutor. java: 624) [na: 1.8.0_221] в орг. apache .tomcat.util.threads.TaskThread $ WrappingRunnable.run (TaskThread. java: 61) [tomcat-embed-core-8.5.39.jar: 8.5.39] в java .lang.Thread.run (Thread. java: 748) [na: 1.8.0_221]
> Я не знаю, можно ли таким образом добавить новый объект в "topicList". Может кто-нибудь помочь?