将具有嵌套对象列表的JSON传递给spring中的rest控制器时出错

时间:2015-09-09 16:54:23

标签: java json spring-mvc

请问我试图将这个json传递给spring框架中的控制器。

{
"date" : "2012-02-09",
"subject" : "Margin ",
"selections" : [ 
                    {"FGY" : ["Try", "Harder"]},{"LGY" : ["Harder", "Try"]}   
               ]
}

选择类是

    import org.springframework.context.annotation.Bean;
    import org.springframework.stereotype.Component;

    import java.io.Serializable;
    import java.util.List;

    @Component
    public class Selections implements Serializable {

    //@Bean
    public Selections(){

    }

    @JsonProperty("FGY")
    private List<String> FGY;

    @JsonProperty("LGY")
    private List<String> LGY;

    public List<String> getFGY() {
        return FGY;
    }

    public void setFGY(List<String> FGY) {
        this.FGY = FGY;
    }

    public String getLGY(){
        return LGY;
    }

    public void setLGY(List<String> LGY) {
        this.LGY = LGY;
    }
}

ReportRequest类是:

   import org.springframework.beans.factory.annotation.Autowired;
   import org.springframework.context.annotation.Bean;
   import org.springframework.stereotype.Component;

   import java.io.Serializable;
   import java.util.Date;
   import java.util.List;

   @Component
   public class ReportRequest implements Serializable{

private String report;
private Date date;
private List<Selections> selections;

public ReportRequest(){

}

public String getReport() {
    return report;
}

public void setReport(String report) {
    this.report = report;
}

public Date getDate() {
    return date;
}

public void setDate(Date date) {
    this.date = date;
}

public List<Selections> getSelections() {
    return selections;
}

public void setSelections(List<Selections> selections) {
    this.selections = selections;
}
public ReportResponse processRequest() {

    for(Selections selection : getSelections()){ // null error occurs on either FGY or LGY depending on which is the second in the json list
        selection.getFGY().forEach(System.out::println); 
        selection.getLGY().forEach(System.out::println);
    }
    return null;
}

控制器

@RequestMapping(value = "/request_report" , method = RequestMethod.POST )
public @ResponseBody
ReportResponse receive(@RequestBody ReportRequest reportRequest)  {
    return reportRequest.processRequest();
}

这是错误:java.lang.NullPointerException:null

执行调试我能够发现ReportRequest processRequest方法中出现null错误。

OLD ERROR(已解决):reportRequest对象的其他属性已从json正确初始化,但属性“selections”无法使用json中的字符串列表初始化“FGY”对象

EDIT(NEW ERROR):新错误是,只有json中选择列表中的第一个对象被传递到spring类ReportRequest中的选择列表中。列表中的第二个对象未通过。因此,在json中以秒为准发生null错误。例如,LGY目前排名第二。

StackTrace:

`   
java.lang.NullPointerException: null
    at com.teamapt.alm.utils.ReportRequest.processRequest(ReportRequest.java:79)
    at com.teamapt.alm.controller.AlmController.receive(AlmController.java:66)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:776)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:705)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:868)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    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.springframework.boot.actuate.autoconfigure.EndpointWebMvcAutoConfiguration$ApplicationContextHeaderFilter.doFilterInternal(EndpointWebMvcAutoConfiguration.java:291)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at com.teamapt.alm.config.Config$1.doFilter(Config.java:36)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.springframework.boot.actuate.trace.WebRequestTraceFilter.doFilterInternal(WebRequestTraceFilter.java:102)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:85)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.springframework.boot.actuate.autoconfigure.MetricFilterAutoConfiguration$MetricsFilter.doFilterInternal(MetricFilterAutoConfiguration.java:90)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    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:501)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:516)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1086)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:659)
    at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:223)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1558)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1515)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

2 个答案:

答案 0 :(得分:2)

给出getter / setter组合

public List<String> getFGY() {
    return FGY;
}

public void setFGY(List<String> FGY) {
    this.FGY = FGY;
}

Jackson(我假设你正在使用的JSON序列化/反序列化)将确定该属性的名称为fgy,而不是FGY。因此,反序列化将无法找到您的财产。

您可以使用

注释其中任何一个
@JsonProperty("FGY")

显式设置您期望的属性名称。

答案 1 :(得分:0)

尝试在父级集合的子类中使用@JsonIgnore。它试图递归地序列化。

请点击此链接了解其他方法。