我正在尝试构建一个RESTFUL Web服务但是我收到的错误是我的代码:
JSP
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<style>
<%@ include file="../css/forms.css" %>
</style>
<script type="text/javascript">
<%@ include file="../js/off_reg.js"%>
$(document).ready(function(){
$('#userName').blur(function(evt){
CheckAvailability();
});
});
function CheckAvailability(){
$.ajax( {
type:'GET', //Could be 'get' depending on your needs
url:'validateUserName.htm',
data:{userName:$('#userName').val()},
dataType: 'json',
success:function(data) {
alert(data);
}
});
}
</script>
DAO
public boolean OfficerExist(String userName){
try{
logger.debug("About to check if officers existing");
String sql = "SELECT userName FROM crimetrack.tblofficers WHERE userName = ?";
logger.info("at this point 1");
//String dbUserName = (String)results.get("userName");
String dbUserName = (String)getJdbcTemplate().queryForObject(sql, new Object[]{userName},String.class);
logger.info("after JdbcTemplate");
if (dbUserName.equals(userName)) {
logger.info("User Name Exists");
return true;
}else{
logger.info("User Name Does NOT Exists");
return false;
}
}catch(Exception e){
logger.info(e.getMessage());
return false;
}
}
FireBug控制台错误:
GET http://localhost:8084/crimeTrack/validateUserName.htm?userName=hello
500 Internal Server Error
70ms
jquery.min.js (line 2)
"NetworkError: 500 Internal Server Error - http://localhost:8084/crimeTrack /validateUserName.htm?userName=hello"
TomCat错误日志:
com.crimetrack.web.OfficerRegistrationController.validateUserName(java.lang.String,com.crimetrack.business.Officers,org.springframework.validation.BindingResult,org.springframework.ui.ModelMap) throws java.lang.Exception
306270 [http-8084-1] DEBUG com.crimetrack.web.OfficerRegistrationController - Inside Controller validateUserName
306270 [http-8084-1] DEBUG com.crimetrack.web.OfficerRegistrationController - The user name that came in is hello
306270 [http-8084-1] INFO com.crimetrack.service.ValidateUserNameManager - Inside Do UserNameExist
306270 [http-8084-1] DEBUG com.crimetrack.jdbc.JdbcOfficersDAO - About to check if officers existing
306270 [http-8084-1] INFO com.crimetrack.jdbc.JdbcOfficersDAO - User Name Found 1
306270 [http-8084-1] INFO com.crimetrack.jdbc.JdbcOfficersDAO - User Name Found 2
306270 [http-8084-1] DEBUG org.springframework.jdbc.core.JdbcTemplate - Executing prepared SQL query
306270 [http-8084-1] DEBUG org.springframework.jdbc.core.JdbcTemplate - Executing prepared SQL statement [SELECT userName FROM crimetrack.tblofficers WHERE userName = ?]
306270 [http-8084-1] DEBUG org.springframework.jdbc.datasource.DataSourceUtils - Fetching JDBC Connection from DataSource
306285 [http-8084-1] DEBUG org.springframework.jdbc.core.StatementCreatorUtils - Setting SQL statement parameter value: column index 1, parameter value [hello], value class [java.lang.String], SQL type unknown
306289 [http-8084-1] DEBUG org.springframework.jdbc.datasource.DataSourceUtils - Returning JDBC Connection to DataSource
306289 [http-8084-1] INFO com.crimetrack.jdbc.JdbcOfficersDAO - Incorrect result size: expected 1, actual 0
306289 [http-8084-1] DEBUG org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver - Resolving exception from handler [com.crimetrack.web.OfficerRegistrationController@a32cdd]: java.lang.IllegalArgumentException: Invalid handler method return value: false
306289 [http-8084-1] DEBUG org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver - Resolving exception from handler [com.crimetrack.web.OfficerRegistrationController@a32cdd]: java.lang.IllegalArgumentException: Invalid handler method return value: false
306289 [http-8084-1] DEBUG org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - Resolving exception from handler [com.crimetrack.web.OfficerRegistrationController@a32cdd]: java.lang.IllegalArgumentException: Invalid handler method return value: false
306290 [http-8084-1] DEBUG org.springframework.web.servlet.DispatcherServlet - Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade@1cbab62
306290 [http-8084-1] DEBUG org.springframework.web.servlet.DispatcherServlet - Could not complete request
java.lang.IllegalArgumentException: Invalid handler method return value: false
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodInvoker.getModelAndView(AnnotationMethodHandlerAdapter.java:971)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:438)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)
306290 [http-8084-1] DEBUG org.springframework.web.context.support.XmlWebApplicationContext - Publishing event in WebApplicationContext for namespace 'crimetrack-servlet': ServletRequestHandledEvent: url=[/crimeTrack/validateUserName.htm]; client=[127.0.0.1]; method=[GET]; servlet=[crimetrack]; session=[null]; user=[null]; time=[28ms]; status=[failed: java.lang.IllegalArgumentException: Invalid handler method return value: false]
306290 [http-8084-1] DEBUG org.springframework.web.context.support.XmlWebApplicationContext - Publishing event in Root WebApplicationContext: ServletRequestHandledEvent: url=[/crimeTrack/validateUserName.htm]; client=[127.0.0.1]; method=[GET]; servlet=[crimetrack]; session=[null]; user=[null]; time=[28ms]; status=[failed: java.lang.IllegalArgumentException: Invalid handler method return value: false]
OfficerRegistrationController
package com.crimetrack.web;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import org.apache.log4j.Logger;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
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 org.springframework.web.servlet.ModelAndView;
import com.crimetrack.business.Login;
import com.crimetrack.business.Officers;
import com.crimetrack.service.DivisionManager;
import com.crimetrack.service.GenderManager;
import com.crimetrack.service.OfficerRegistrationValidation;
import com.crimetrack.service.PositionManager;
import com.crimetrack.service.ValidateUserNameManager;
@Controller
public class OfficerRegistrationController {
private final Logger logger = Logger.getLogger(getClass());
private DivisionManager divisionManager;
private PositionManager positionManager;
private GenderManager genderManager;
private Officers officer = new Officers();
private ValidateUserNameManager validateUserNameManager;
Map<String, Object> myDivision = new HashMap<String, Object>();
Map<String, Object> myPosition = new HashMap<String, Object>();
Map<String, Object> myGender = new HashMap<String, Object>();
OfficerRegistrationValidation validateData = new OfficerRegistrationValidation();
@InitBinder("officers")
protected void initBinder(WebDataBinder binder){
//removes white spaces
binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
//formats date
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
//By passing true this will convert empty strings to null
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
dateFormat.setLenient(false);
binder.setValidator(new OfficerRegistrationValidation());
}
@RequestMapping(value="officer_registration.htm", method = RequestMethod.GET)
public ModelAndView loadPage(HttpServletRequest request,
HttpServletResponse response,@ModelAttribute Officers officer, BindingResult result, ModelMap m, Model model) throws Exception {
try{
logger.debug("In Http method for OfficerRegistrationController");
myDivision.put("divisionList", this.divisionManager.getDivisions());
myPosition.put("positionList", this.positionManager.getPositionList());
myGender.put("genderList", this.genderManager.getGenderList());
model.addAttribute("division", myDivision);
model.addAttribute("position", myPosition);
model.addAttribute("gender", myGender);
return new ModelAndView("officer_registration");
}catch(Exception e){
request.setAttribute("error",e.getMessage());
return new ModelAndView("error_page");
}
}
@RequestMapping(value="officer_registration.htm", method=RequestMethod.POST)
public ModelAndView handleRequest(@Valid @ModelAttribute Officers officer, BindingResult result, ModelMap m, Model model)throws Exception{
if(result.hasErrors()){
model.addAttribute("division", myDivision);
model.addAttribute("position", myPosition);
model.addAttribute("gender", myGender);
return new ModelAndView("officer_registration");
}else{
return null;
}
}
@RequestMapping(value="validateUserName.htm", method=RequestMethod.GET)
public boolean validateUserName(@RequestParam String userName, @ModelAttribute Officers officer, BindingResult result, ModelMap m)throws Exception{
try{
logger.debug("Inside Controller validateUserName");
logger.debug("The user name that came in is " + userName);
if (validateUserNameManager.DoesUserNameExist(userName)== true){
return true;
}
return false;
// return true;
}catch(Exception e){
logger.debug("Error in validateUserName Controller " + e.getMessage());
return false;
}
}
public void setDivisionManager(DivisionManager divisionManager){
this.divisionManager = divisionManager;
}
public void setPositionManager(PositionManager positionManager){
this.positionManager = positionManager;
}
public void setGenderManager(GenderManager genderManager){
this.genderManager = genderManager;
}
/**
* @return the validateUserNameManager
*/
public ValidateUserNameManager getValidateUserNameManager() {
return validateUserNameManager;
}
/**
* @param validateUserNameManager the validateUserNameManager to set
*/
public void setValidateUserNameManager(
ValidateUserNameManager validateUserNameManager) {
this.validateUserNameManager = validateUserNameManager;
}
/**
* @return the officer
*/
public Officers getOfficer() {
return officer;
}
/**
* @param officer the officer to set
*/
public void setOfficer(Officers officer) {
this.officer = officer;
}
}
答案 0 :(得分:1)
您似乎正在运行此查询:
SELECT userName FROM crimetrack.tblofficers WHERE userName = 'hello'
查询成功运行,但匹配userName = 'hello'
的表中没有记录,因此返回空结果集。然后创建该官员失败,因为没有要创建的官员并且没有处理该条件。
独立运行查询并确认没有此类记录。然后查看您的映射代码,看看应该如何处理这个条件。
跟进... JdbcTemplate.queryForObject州的java文档:
此方法对于运行具有已知结果的静态SQL非常有用。该 查询应该是单行/单列查询;归来的 result将直接映射到相应的对象类型。
...
抛出:IncorrectResultSizeDataAccessException - 如果查询没有 只返回一行,或者不返回其中的一列 行
所以如果你不确定数据库中的记录,就不应该调用它,否则行:
String dbUserName = (String)getJdbcTemplate().queryForObject(sql, new Object[]{userName},String.class);
需要用try-catch包装来处理这种情况。