首先测试接口是不是有错误测试后台接口函数 测试目录放在和application.xml同一级
import com.wfbql.club.dao.IUserDao; import com.wfbql.club.daomain.IUserLogin; import com.wfbql.club.service.IUservice; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.util.List; //spring整合了junit,同时 只有spring引入外部文件时需要classpath @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:applicationContext.xml") public class TestDemo1 { @Autowired private com.wfbql.club.service.IUservice IUservice; @Test public void test1() throws Exception { IUserLogin t1 = new IUserLogin(); IUservice.test(t1); } }
发现接口正常查询使用 那么肯定是controller层写错了,定位错误位置
import com.wfbql.club.daomain.IUserLogin; import com.wfbql.club.service.IUservice; import com.wfbql.club.service.impl.IUserviceImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import javax.servlet.http.HttpServletRequest; import java.io.IOException; import java.util.HashMap; import java.util.Map; @Controller @RequestMapping("/user") public class IUserController { @Autowired private IUservice iUservice; @ResponseBody @RequestMapping(value ="/login.do",method = RequestMethod.POST) public Map<String, Object> Login(HttpServletRequest request,@RequestParam("username") String username, @RequestParam("password") String password) throws Exception { Map<String,Object> map = new HashMap<String, Object>(); System.out.println(username+"===="+password); IUserLogin iuser = new IUserLogin(); iuser.setUsername(username); iuser.setPassword(password); iUservice =new IUserviceImpl(); iUservice.test(iuser); map.put("json",iuser); return map; }}
发现自己写错了 iUservice =new IUserviceImpl(); 没有使用@Autowired来生成实现类 ,去掉iUservice =new IUserviceImpl(); 即可正常运行.但是为什么是报的空指针还是不知道,错误的位置反复查看了也没有错.很难去发现.
主要还是需要测试函数来进行排查,确定报错位置