错误 404 spring v2.3.2 在使用 Ajax Axios 发送 POST 请求时在 Tomcat 8 部署时发生。

huangapple go评论67阅读模式
英文:

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

问题

我的Spring应用在本地机器上运行良好,并成功部署在Tomcat 8和9上,但是当通过axios进行ajax数据提交时,我收到404错误消息。我已经检查了前端和后端的URL,它们都没有问题。

控制器:

@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>

请帮助我,因为我觉得可能有些地方我漏掉了。

英文:

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=&quot;/csis/ajax/student/search&quot;, 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

&lt;script th:inline=&quot;javascript&quot;&gt;

    new Vue({
        el: &quot;#student_details&quot;,

        data:{
            studentNumber: &#39;&#39;,
            search_content: false,
            result_set: false,
            student: []
        },



        methods:{

            searchStudent: function () {

                this.search_content = false;

                axios.post(&quot;/csis/ajax/student/search&quot;,{
                    studentNumber: this.studentNumber
                }).then(response =&gt;{

                  

                     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 =&gt;{
                    console.log(error)
                })
            }

        }


    })
&lt;/script&gt;

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

答案1

得分: 1

您的应用程序可能在嵌入式Web服务器上运行良好,但在外部Tomcat服务器上可能无法按预期工作,因为在外部Tomcat服务器上,JavaScript方法将调用 localhost:8080/csis/ajax/student/search,但 searchStudent 方法将通过 localhost:8080/{root-contextpath}/csis/ajax/student/search 访问,而不是 localhost:8080/csis/ajax/student/search

要更正上述问题,请确保在您的属性文件中添加以下行:

server.servlet.context-path=/csis

并将@RequestMapping的值更改如下:

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

}

需要注意的关键事项是:

Spring Boot应用程序默认在根上下文路径(“/”)上提供内容

现在,由于更喜欢约定而不是配置是一个好主意,有些情况下确实需要定义自己的自定义路径,您的情况就是一个完美的例子。

还要记住将Tomcat服务器依赖项设置为提供并在某些Spring Boot库中排除它,如下所示:

<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>
        <!-- 通过 @ASopia: 排除Tomcat依赖项 -->
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>
英文:

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=&quot;/ajax/student/search&quot;, 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:

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

huangapple
  • 本文由 发表于 2020年7月30日 17:06:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/63169822.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定