为什么spring controller,方法POST转换DTO类时会抛出null异常?

时间:2018-02-02 22:03:24

标签: spring spring-mvc

酒:

@Entity
@Table
public class Wine {

    @Id
    @GeneratedValue
    @Column
    private Long id;
    @Column
    private String name;
    @Column
    private Double alcoholPercent;
    @Column
    private Double price;
    @Column
    private int quantity;
    @Column
    private Date vintage;
    @ManyToOne(fetch=FetchType.EAGER)
    private Company company;
    @ManyToOne(fetch=FetchType.EAGER)
    private Type type;
}

Wine DTO:

public class WineDTO {
    private Long id;
    private String name;
    private Double alcoholPercent;
    private Double price;
    private int quantity;
    private Date vintage;
    private Long companyId;
    private String companyName;
    private String companyPib;
    private String companyCountry;
    private Long typeId;
    private String typeName;
}

WineToWineDTO:

@Component
public class WineToWineDTO 
    implements Converter<Wine, WineDTO>{

    @Override
    public WineDTO convert(Wine c) {
        WineDTO dto=new WineDTO();
        dto.setId(c.getId());
        dto.setName(c.getName());
        dto.setAlcoholPercent(c.getAlcoholPercent());
        dto.setPrice(c.getPrice());
        dto.setQuantity(c.getQuantity());
        dto.setVintage(c.getVintage());
        dto.setCompanyId(c.getCompany().getId());
        dto.setCompanyName(c.getCompany().getName());
        dto.setCompanyPib(c.getCompany().getPib());
        dto.setCompanyCountry(c.getCompany().getCountry());
        dto.setTypeId(c.getType().getId());
        dto.setTypeName(c.getType().getName());


        return dto;
    }

    @RequestMapping(method=RequestMethod.POST)
    public ResponseEntity<WineDTO> add(
            @RequestBody WineDTO newWine){

        Wine wine = toWine.convert(newWine); 
        wineService.save(wine);

        return new ResponseEntity<>(toDTO.convert(wine),
                HttpStatus.CREATED);
    }
}

当我请求发布新对象时,我的控制器会发布除了类型ID之外的所有列。有人能告诉我哪里弄错了吗?感谢。

  

java.lang.NullPointerException:null       在org.wineShop.wine.support.WineToWineDTO.convert(WineToWineDTO.java:28)       在org.wineShop.wine.web.controller.ApiWineController.add(ApiWineController.java:61)       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)       at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)       at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)       at java.lang.reflect.Method.invoke(Unknown Source)       在org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)       在org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)       在org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)       在org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:775)       在org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:705)       在org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)       在org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)       在org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)       在org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:967)       在org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:869)       在javax.servlet.http.HttpServlet.service(HttpServlet.java:648)       在org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843)       在javax.servlet.http.HttpServlet.service(HttpServlet.java:729)       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)       在org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)       在org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)       在org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)       在org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:85)       在org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)       在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:217)       在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)       在org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)       在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)       at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)       在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)       在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)       在org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)       at org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:673)       在org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1500)       在org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.run(NioEndpoint.java:1456)       at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)       at java.util.concurrent.ThreadPoolExecutor $ Worker.run(Unknown Source)       at org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)       在java.lang.Thread.run(未知来源)

0 个答案:

没有答案