java - HTTP 状态 400 错误请求

标签 java rest

我正在开发一个 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/

相关文章:

node.js - 使用 Express 传递 json 正文调用现有 API

java - 将java语言映射到数据库

javascript - 将新数据从 Node REST API 推送到 React-Native

java - 如何使用 REST API - Java 从 SharePoint 2010 获取更新的数据?

spring - 使用 Spring Integration 客户端的持久连接/连接重用

java - 更改我的 android 应用程序包名称是否会更改我的应用程序的 sha1 代码?

java - 什么是NullPointerException,我该如何解决?

java打印到文件削减数字的最后一位

Java 2D 游戏 - MouseClicked 和 MouseMoved 方法

rest - Advanced REST Client for Chrome 中的 SSL 证书