英文:
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="/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.
答案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="/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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论