Search code examples
javascriptjavaspring-bootvue.jsthymeleaf

error 404 spring v2.3.2 when posting with ajax axios on tomcat 8 deployment


My spring application works well in my local machine and deploys successfully on tomcat 8 and 9 but when posting data with ajax via axios i am getting a 404 error messages, i have checked the url on both front-end and back-end and they are okay.

CONTROLLER

@Controller
public class StudentController {

@Autowired
StudentSearchRepo studentSearchRepo;

@ResponseBody
@RequestMapping(value="/csis/ajax/student/search", method = RequestMethod.POST)
public StudentSearch searchStudent(@RequestBody Student student)
{

    StudentSearch studentSearch = studentSearchRepo.findByStudentNumber(student.getStudentNumber());

    if(studentSearch == null)
    {
        StudentSearch studentSearch1 = new StudentSearch();
        studentSearch1.setApplicantNumber(1);
        return studentSearch1;
    }
    return studentSearch;

}
}

JAVASCRIPT

<script th:inline="javascript">

    new Vue({
        el: "#student_details",

        data:{
            studentNumber: '',
            search_content: false,
            result_set: false,
            student: []
        },



        methods:{

            searchStudent: function () {

                this.search_content = false;

                axios.post("/csis/ajax/student/search",{
                    studentNumber: this.studentNumber
                }).then(response =>{

                  

                     if(response.data.applicantNumber === 1)
                     {
                       
                         this.search_content = false;
                         this.result_set = true;

                     }else {
                         this.search_content = true;
                         this.result_set = false;
                         this.student = response.data
                      

                     }
                  
                }).catch( error =>{
                    console.log(error)
                })
            }

        }


    })
</script>

kindly help because i feel there is something i might be missing.


Solution

  • Your application might work well on an Embedded Web Server and not work as expected on an external tomcat server because on an external tomcat server the javascript method would be calling localhost:8080/csis/ajax/student/search but the searchStudent method would be accessible through localhost:8080/{root-contextpath}/csis/ajax/student/search and not localhost:8080/csis/ajax/student/search

    To correct above ensure you have below line in your properties file

    server.servlet.context-path=/csis
    

    And change the value of @RequestMapping as below

        @ResponseBody
        @RequestMapping(value="/ajax/student/search", method = RequestMethod.POST)
        public StudentSearch searchStudent(@RequestBody Student student)
        {
    
            StudentSearch studentSearch = studentSearchRepo.findByStudentNumber(student.getStudentNumber());
    
            if(studentSearch == null)
            {
                StudentSearch studentSearch1 = new StudentSearch();
                studentSearch1.setApplicantNumber(1);
                return studentSearch1;
            }
            return studentSearch;
    
        }
    

    A key thing to note is:

    Spring Boot Applications, by default, serves content on the root context path (“/”)

    Now, since it's a good idea to prefer convention over configuration, there are some cases where we really need to define our own custom path and you case is a perfect example.

    Also remember to set the Tomcat server dependency to provide and exclude it in some spring boot libraries as below:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <!-- By @ASopia: Exclude the Tomcat dependency-->
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>