Spring WebMVC WebService数据存储自动装配为null

时间:2017-03-22 09:29:41

标签: java spring web-services

我正在使用WebMVC并按类生成Web服务。 Web服务响应很好,但是,在数据存储时获得零点异常。

Web服务类,piDNHeaderDAO1获取空点异常

public class wm011DNinfo {

    @Autowired
    private PIDNHeaderDAO piDNHeaderDAO1;

    public OUTDNResponse[] processGenerate(INDNHeader[] inDNHeaders, INDNItem[] inDNItems) {


        //for data store
        PIDNHeader dnHeader = new PIDNHeader();
        dnHeader.setVbeln("test1");
        dnHeader.setVstel("test2");
        System.out.println("@ "+piDNHeaderDAO1);
        //System.out.println("@ "+piDNHeaderDAO2);
        piDNHeaderDAO1.saveOrUpdate(dnHeader);

        //for response
        OUTDNResponse[] outDNarray = new OUTDNResponse[1];
        OUTDNResponse outDN = new OUTDNResponse();

        outDN.setVbeln("OK");
        outDN.setZzint_msg("S");
        outDNarray[0] = outDN;

        return outDNarray;
    }

}

PIDNHeaderDAO

package net.viralpatel.spring.dao;

import java.util.List;
import net.viralpatel.spring.model.PIDNHeader;

public interface PIDNHeaderDAO {
    public void saveOrUpdate(PIDNHeader dnHeader);

    public void delete(String vbeln);

    public PIDNHeader get(String vbeln);

    public List<PIDNHeader> list();
}

PIDNHeaderDAOImpl

package net.viralpatel.spring.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.sql.DataSource;

import net.viralpatel.spring.model.PIDNHeader;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.RowMapper;

public class PIDNHeaderDAOImpl implements PIDNHeaderDAO {

    private JdbcTemplate jdbcTemplate;

    public PIDNHeaderDAOImpl(DataSource dataSource) {
        jdbcTemplate = new JdbcTemplate(dataSource);
    }

    public void saveOrUpdate(PIDNHeader dnHeader) {
        // TODO Auto-generated method stub
        String sql = "INSERT INTO PIDNHeader " + "(vbeln, vstel) VALUES (?, ?)";
        jdbcTemplate.update(sql, dnHeader.getVbeln(), dnHeader.getVstel());


    }

    public void delete(String vbeln) {
        // TODO Auto-generated method stub
        String sql = "DELETE FROM PIDNHeader WHERE vbeln=?";
        jdbcTemplate.update(sql, vbeln);
    }

    public PIDNHeader get(String vbeln) {
        // TODO Auto-generated method stub
        String sql = "SELECT * FROM PIDNHeader WHERE vbeln=" + vbeln;
        return jdbcTemplate.query(sql, new ResultSetExtractor<PIDNHeader>() {

            public PIDNHeader extractData(ResultSet rs) throws SQLException,
                    DataAccessException {
                if (rs.next()) {
                    PIDNHeader dnHeader = new PIDNHeader();

                    dnHeader.setVbeln(rs.getString("vbeln"));
                    dnHeader.setVstel(rs.getString("vstel"));

                    return dnHeader;
                }

                return null;
            }

        });
    }

    public List<PIDNHeader> list() {
        // TODO Auto-generated method stub
        String sql = "SELECT * FROM PIDNHeader";
        List<PIDNHeader> listDNHeader = jdbcTemplate.query(sql, new RowMapper<PIDNHeader>() {

            public PIDNHeader mapRow(ResultSet rs, int rowNum) throws SQLException {
                PIDNHeader dnHeader = new PIDNHeader();

                dnHeader.setVbeln(rs.getString("vbeln"));
                dnHeader.setVstel(rs.getString("vstel"));

                return dnHeader;
            }

        });

        return listDNHeader;
    }

}

和AppConfig.java

package net.viralpatel.spring.config;
import javax.sql.DataSource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

import net.viralpatel.spring.dao.PIDNHeaderDAO;
import net.viralpatel.spring.dao.PIDNHeaderDAOImpl;


@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "net.viralpatel.spring")
public class AppConfig extends WebMvcConfigurerAdapter {
    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/WEB-INF/views/");
        viewResolver.setSuffix(".jsp");

        System.out.println("appconfig start");

        return viewResolver;
    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    }


    @Bean
    public DataSource getDataSource() {

        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        dataSource.setUrl("jdbc:sqlserver://192.168.4.22;DatabaseName=xxxx");
        dataSource.setUsername("xxxxx");
        dataSource.setPassword("accxxxxxpac");

        return dataSource;
    }


    @Bean
    public PIDNHeaderDAO getPIDNHeaderDAO() {
        System.out.println("PIDNHeaderDAO start");
        return new PIDNHeaderDAOImpl(getDataSource());
    }

}

通过eclipse web服务浏览器调用Web服务后,获取以下消息:

<soapenv:Fault>
  <soapenv:Fault>
    <faultcode>soapenv:ServeruserException</faultcode>
      <faultstring>java.lang.NullPointerException</faultstring>
        <detail>
          <nsl:hostname xmlns:ns1="http://xml.apache.org/axis/">home.local</nsl:hostname>
        </detail>
      </soapenv:Fault>
    </soapenv:Body>
</soapenv:Envelope>

1 个答案:

答案 0 :(得分:0)

wm011DNinfo类不是Spring bean。因此,您不能通过PIDNHeaderDAO将其他bean注入其中(@Autowired)。使用类似@Component的内容或手动创建带@Bean注释的方法(就像getPIDNHeaderDAO方法一样)。