Konvertieren Sie ein Objekt in JSON-REST-service von Spring MVC

Ich versuche zu schaffen, einen REST service mit Spring MVC und es funktioniert, wenn ich fahre ein einfacher string. Meine Forderung ist die Rückgabe eines JSON-Strings, der von dem Java-Objekt. Weiß nicht, wie das zu erreichen ist durch die implizite Konvertierung.

Hier ist mein code:

StudentService.java

package com.spring.schoolmanagement.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.spring.schoolmanagement.dao.CourseDAOImpl;
import com.spring.schoolmanagement.dao.StateDAOImpl;
import com.spring.schoolmanagement.dao.StudentDAOImpl;
import com.spring.schoolmanagement.model.Student;

@Controller
@RequestMapping("/rest/student")
public class StudentService {

    @Autowired
    private CourseDAOImpl courseService;
    @Autowired
    private StudentDAOImpl studentService;
    @Autowired
    private StateDAOImpl stateService;


    @RequestMapping(value = "/{id}", method = RequestMethod.GET, headers = "Accept=*/*")
    @ResponseBody
    public Student home(@PathVariable int id) {
        return this.studentService.getById(id);
    }

    @RequestMapping(method = RequestMethod.GET, headers = "Accept=*/*")
    @ResponseBody
    public List<Student> getAll() throws Exception {
        return this.studentService.getAll();
    }

    @RequestMapping(value = "/test", method = RequestMethod.GET, headers = "Accept=*/*")
    @ResponseBody
    public String test() {
        return "Test REST Service!!!";
    }
}

Student.java

package com.spring.schoolmanagement.model;

import java.util.Date;

import javax.validation.constraints.Size;

import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.format.annotation.DateTimeFormat;

public class Student extends Contact{
    private int id;

    @NotEmpty
    @Size(max = 30)
    private String firstName, lastName;
    //private String lastName;

    @DateTimeFormat(pattern="MM/dd/yyyy")
    private Date DOB, DOA;
    //private Date DOA;

    @NotEmpty
    @Email
    private String email;
    private String password;
    private int courseID;
    private String courseName;

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    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 Date getDOB() {
        return DOB;
    }
    public void setDOB(Date dOB) {
        DOB = dOB;
    }
    public Date getDOA() {
        return DOA;
    }
    public void setDOA(Date dOA) {
        DOA = dOA;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public int getCourseID() {
        return courseID;
    }
    public void setCourseID(int courseID) {
        this.courseID = courseID;
    }
    public String getCourseName() {
        return courseName;
    }
    public void setCourseName(String courseName) {
        this.courseName = courseName;
    }
}

Hier http://localhost:8080/schoolmangement/rest/student/test URL ist wieder in "Test-REST-Dienst!!!"

Aber, http://localhost:8080/schoolmangement/rest/student/1 URL werfen HTTP-Status-code 406 mit Fehlermeldung:

Die Ressource identifiziert, die durch diese Anforderung ist nur in der Lage, generieren von Antworten mit Merkmalen, die nicht akzeptabel nach der Anfrage "accept" - Header.

  • Ich weiß nicht, warum Sie angeben headers = "Accept=*/*", aber Sie sollten geben Sie produces = "application/json"
  • Spring-Konfiguration wohl keine Jackson-mapper. Nutzen Spring-Boot-Konfiguration wird die Konfiguration automatisch, und wenn Sie nicht mit einer entkoppelten DTO für Ihre externe API-Darstellung, berücksichtigen Spring Data REST.
  • Ja, du hast Recht. Endlich hab ich die Lösung.
InformationsquelleAutor Roul | 2015-03-16
Schreibe einen Kommentar