1

Java 8 を使用して Spring Boot バージョン 2 に取り組んでおり、「createTopic」メソッドで新しい List オブジェクトを定義済みの List オブジェクト (つまり topicList) に追加しようとすると、エラーが発生します。

**

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

**

TopicObject.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 ERROR 15312 --- [nio-8080-exec-2] oaccC[.[.[/].[dispatcherServlet] : サーブレット [dispatcherServlet] の Servlet.service() とのコンテキストでパス [] が例外をスローしました [リクエストの処理に失敗しました。ネストされた例外は、根本原因を持つ java.lang.UnsupportedOperationException] です

java.lang.UnsupportedOperationException: java.util.AbstractList.add(AbstractList.java:148) で null ~[na:1.8.0_221] java.util.AbstractList.add(AbstractList.java:108) で ~[na:1.8] .0_221] io.javabrains.springBootStarter.Service.TopicService.createTopic(TopicService.java:29) ~[classes/:na] at io.javabrains.springBootStarter.controller.TopicController.createTopic(TopicController.java:31) ~[ classes/:na] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_221] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_221] at sun. .reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_221] at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_221] at org.springframework .web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:209) ~[spring-web-5.0.13.RELEASE.jar:5.0.13.RELEASE] 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- webmvc-5.0.13.RELEASE.jar:5.0.13.RELEASE] at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:894) ~[spring-webmvc-5.0.13 .RELEASE.jar:5.0.13.RELEASE] at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:800) ~[spring-webmvc-5.0.13.RELEASE.jar:5.0.13.RELEASE] at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.0.13.RELEASE.jar:5.0.13 .RELEASE] at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991) ~[spring-webmvc-5.0.13.RELEASE.jar:5.0.13.RELEASE] at org.springframework.web.servlet .DispatcherServlet.doService(DispatcherServlet.java:925) ~[spring-webmvc-5.0.13.RELEASE.jar:5.0.13.RELEASE] at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:981) ~[spring-webmvc-5.0.13.RELEASE.jar:5.0.13.RELEASE] at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:884) ~[spring-webmvc-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] org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:858) で ~[spring-webmvc-5.0.13 .RELEASE.jar:5.0.13.RELEASE] at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) ~[tomcat-embed-core-8.5.39.jar:8.5.39] at org.apache .catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[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] org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) で ~[tomcat-embed-websocket-8.5 .39.jar:8.5.39] 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] 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] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.39.jar: 8.5.39] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.39.jar:8.5.39] at org.springframework.web.filter. HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:109) ~[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] at org .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.RELEASE] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.0 .13.RELEASE.jar:5.0.13.RELEASE] 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] at 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] org.apache.catalina.org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) で [ tomcat-embed-core-8.5.39.jar:8.5.39] org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:493) [tomcat-embed-core-8.5.39.jar:8.5. 39] org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137) で [tomcat-embed-core-8.5.39.jar:8.5.39] org.apache.catalina.valves.ErrorReportValve.invoke で(ErrorReportValve.java:81) [tomcat-embed-core-8.5.39.jar:8.5.39] org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) [tomcat-embed-core- 8.5.39.jar:8.5.39] org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) [tomcat-embed-core-8.5.39.jar:8.5.39] org.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] org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1498) で [tomcat -embed-core-8.5.39.jar:8.5.39] org.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] org.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」に新しいオブジェクトを追加できるかどうかはわかりません。誰か助けてくれませんか?

4

2 に答える 2