SpringMVC Restful-Web-Service

Ich versuche zu bauen, ein RESTFUL-Web-Service jedoch erhalte ich eine Fehlermeldung unter ist mein code:

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-Konsole Fehler:

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 Error Log:

    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;
    }


}
Das Protokoll zeigt Fehler, ist der Wert, der zurückgegeben wird, durch die OfficerRegistrationController. Du solltest den code für die Klasse.
Gibson habe ich den Inhalt des Controllers. Ich bin nicht ganz sicher, ob ich den Aufruf des web-service richtig aus der jsp. Irgendwelche Ideen?

InformationsquelleAutor devdar | 2012-09-19

Schreibe einen Kommentar