gpt4 book ai didi

java - HTTP 状态 400 错误请求

转载 作者:行者123 更新时间:2023-12-02 06:21:25 25 4
gpt4 key购买 nike

我正在开发一个 JAVA Restful Web 应用程序。早些时候,所有功能都正常工作。但现在我面临一个错误,说

'HTTP 状态 400 – 错误请求类型状态报告

描述 由于被认为是客户端错误(例如格式错误的请求语法、无效的请求消息帧或欺骗性请求路由),服务器无法或不会处理请求。

Apache Tomcat/8.5.31'

我无法弄清楚我的代码出了什么问题...寻求帮助谢谢!

//////这是我的模块类 - (Student.java)

package com.joseph.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Student {
@Id
@Column
@GeneratedValue(strategy=GenerationType.AUTO) //for autonumber
private int studentId;
@Column
private String firstname;
@Column
private String lastname;
@Column
private int yearLevel;

public Student(){}
public Student(int studentId, String firstname, String lastname,
int yearLevel) {
super();
this.studentId = studentId;
this.firstname = firstname;
this.lastname = lastname;
this.yearLevel = yearLevel;
}
public int getStudentId() {
return studentId;
}
public void setStudentId(int studentId) {
this.studentId = studentId;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public int getYearLevel() {
return yearLevel;
}
public void setYearLevel(int yearLevel) {
this.yearLevel = yearLevel;
}


}

//////DAO 类 (StudentDao.java)

package com.joseph.dao;

import java.util.List;

import com.joseph.model.Student;

public interface StudentDao {
public void add(Student student);
public void edit(Student student);
public void delete(int studentId);
public Student getStudent(int studentId);
public List getAllStudent();
public List searchStudent(String srch);
}

////DAO实现类(StudentDaoImpl.java)

package com.joseph.dao.impl;

import java.util.List;

import javax.sql.DataSource;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.joseph.dao.StudentDao;
import com.joseph.model.Student;

@Repository
public class StudentDaoImpl implements StudentDao {

JdbcTemplate template;

@Autowired
private SessionFactory session;



@Autowired
public void setDatsSource(DataSource dataSource) {
template = new JdbcTemplate(dataSource);
}


@Override
public void add(Student student) {
//session.getCurrentSession().save(student);
String sql = "insert into student(firstname, lastname, yearLevel) values ('"+student.getFirstname()+"', '"+student.getLastname()+"', '"+student.getYearLevel()+"')";
template.update(sql);
}

@Override
public void edit(Student student) {
String sql = "update student set firstname = '"+student.getFirstname()+"', lastname = '"+student.getLastname()+"', yearLevel = '"+student.getYearLevel()+"' where studentId = '"+student.getStudentId()+"'";
template.update(sql);
//session.getCurrentSession().update(student);
}

@Override
public void delete(int studentId) {
String sql = "delete from student where studentId = '"+studentId+"'";
String sql2 = "select count(*) from student";
template.update(sql);
int cnt = template.queryForObject(sql2, Integer.class);
System.out.println(cnt);
}

@Override
public Student getStudent(int studentId) {
return (Student)session.getCurrentSession().get(Student.class, studentId);
}

@Override
public List getAllStudent() {
String sql = "select * from student";
return template.queryForList(sql);
}

@Override
public List searchStudent(String stdID) {
String srch = "%" + stdID + "%";
String sql = "select * from student where studentId like '"+srch+"' OR firstname like '"+srch+"'";
return template.queryForList(sql);
}

}

////学生服务(StudentService.java)

package com.joseph.service;

import java.util.List;

import com.joseph.model.Student;

public interface StudentService {
public void add(Student student);
public void edit(Student student);
public void delete(int studentId);
public Student getStudent(int studentId);
public List getAllStudent();
public List searchStudent(String srch);
}

////学生服务实现(StudentServiceImpl.java) 包 com.joseph.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.joseph.dao.StudentDao;
import com.joseph.model.Student;
import com.joseph.service.StudentService;
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentDao studentDao;

@Transactional
public void add(Student student) {
studentDao.add(student);
}

@Transactional
public void edit(Student student) {
studentDao.edit(student);
}

@Transactional
public void delete(int studentId) {
studentDao.delete(studentId);
}

@Transactional
public Student getStudent(int studentId) {
return studentDao.getStudent(studentId);
}

@Transactional
public List getAllStudent() {
return studentDao.getAllStudent();
}

@Transactional
public List searchStudent(String srch) {
return studentDao.searchStudent(srch);
}

}

// Controller 类/////

package com.joseph.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
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 com.joseph.model.Student;
import com.joseph.service.StudentService;

@Controller
public class StudentController {
@Autowired
private StudentService studentService;

@RequestMapping("/index")
public String setupForm(Map<String, Object> map){
Student student = new Student();
map.put("student", student);
map.put("studentList", studentService.getAllStudent());
return "student";
}
@RequestMapping(value="/student.do", method=RequestMethod.POST)
public String doActions(@ModelAttribute Student student, BindingResult result, @RequestParam String action, @RequestParam String searchVal, Map<String, Object> map){
Student studentResult = new Student();
String opr = action.toLowerCase();

if(opr.equals("add")) {
studentService.add(student);
studentResult = student;

map.put("student", studentResult);
map.put("studentList", studentService.getAllStudent());
return "student";
}

else if(opr.equals("edit")) {
studentService.edit(student);
studentResult = student;

map.put("student", studentResult);
map.put("studentList", studentService.getAllStudent());
return "student";
}

else if(opr.equals("delete")) {
studentService.delete(student.getStudentId());
studentResult = new Student();

map.put("student", studentResult);
map.put("studentList", studentService.getAllStudent());
return "student";
}

else if(opr.equals("load")) {
Student searchedStudent = studentService.getStudent(student.getStudentId());
studentResult = searchedStudent!=null ? searchedStudent : new Student();
map.put("student", studentResult);
return "studentEdit";
}

else if(opr.equals("search")) {
System.out.println(searchVal);
map.put("student", studentResult);
map.put("studentList", studentService.searchStudent(searchVal));
return "student";
}


else {
map.put("student", studentResult);
map.put("studentList", studentService.getAllStudent());
return "student";
}

}

}

//JSP 表单 - Student.jsp///

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ include file="/WEB-INF/jsp/includes.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Student Management</title>
</head>
<body>
<h1>Students Data</h1>
<form:form action="student.do" method="POST" commandName="student">
<table>
<tr>
<td><input type="text" name="searchVal" /></td>
<td><input type="submit" name="action" value="Search" /></td>
</tr>

<tr>
<td>First name</td>
<td><form:input type="text" path="firstname" /></td>
</tr>
<tr>
<td>Last name</td>
<td><form:input type="text" path="lastname" /></td>
</tr>
<tr>
<td>Year Level</td>
<td><form:input type="text" path="yearLevel" /></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" name="action" value="Add" />
</td>
</tr>
</table>
</form:form>
<br>
<table border="1">

<th>ID</th>
<th>First name</th>
<th>Last name</th>
<th>Year level</th>

<c:forEach items="${studentList}" var="student">
<tr>
<td>${student.studentId}</td>
<td>${student.firstname}</td>
<td>${student.lastname}</td>
<td>${student.yearLevel}</td>
<form:form action="student.do" method="POST" commandName="student">
<form:input path="studentId" value="${student.studentId}" hidden="hidden"/>
<td><input type="submit" name="action" value="Load" /></td>
<td><input type="submit" name="action" value="Delete" /></td>
</form:form>
</tr>
</c:forEach>
</table>
</body>
</html>

最佳答案

@Controller 用于将类标记为 Spring MVC Controller,它将响应 View

@RestController 用于 Restful API。它是一个方便的注释,其中包括@Controller和@ResponseBody注释

@Controller // please try to update this line.
public class StudentController {
...
}

更改为

@RestController
public class StudentController {
...
}

关于java - HTTP 状态 400 错误请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55831270/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com