0

I need to execute this URL: http://localhost:8080/FitiProject/student and the response needs to be a json string containing the data of a Student object.

Here is my code:

package spring.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import model.Student;

@Controller
public class PruebaController {
    @RequestMapping("/student")
    public @ResponseBody Student getStudent(){
        return new Student(2, "h");
    }
}

This is Student.java

package model;

public class Student {
    private int edad;
    private String nombre;

    public Student(int edad, String nombre) {
        super();
        this.edad = edad;
        this.nombre = nombre;
    }

    public int getEdad() {
        return edad;
    }
    public void setEdad(int edad) {
        this.edad = edad;
    }
    public String getNombre() {
        return nombre;
    }
    public void setNombre(String nombre) {
        this.nombre = nombre;
    }
}

When I do a GET request on the URL, I don't get a JSON response instead I get a 406 error code. How can I solve this?

I'm using Jackson 1.9 and Spring 4.1.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • 4
    What exactly is your question?? – codeMan Feb 01 '16 at 14:38
  • Estado HTTP 406 -

    type Informe de estado

    mensaje

    descripción El recurso identificado por este requerimiento sólo es capaz de generar respuestas con características no aceptables con arreglo a las cabeceras "accept" de requerimiento.

    – Federico Portillo Feb 01 '16 at 15:30

4 Answers4

2

Your getStudent method lacks the content type of the response, otherwise Spring won't know to which format convert the Student. This can be done by using produces attribute in @RequestMapping.

//"produces" will tell Spring to which format convert the data
//"method" will tell Spring which HTTP method should be handled for this URL
@RequestMapping(value="/student",
    produces="application/json; charset=UTF-8",
    method=RequestMethod.GET)
public @ResponseBody Student getStudent(){
    return new Student(2, "h");
}

When executing a request to your URL, make sure that the client uses the following header: Content-Type: application/json

It's worth to mention that your project needs to have jackson libraries in order to work.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
1

If you're already using Jackson, you could try using the ObjectMapper class:

ObjectMapper mapper = new ObjectMapper();
System.out.println("Object in JSON:\n" + mapper.writerWithDefaultPrettyPrinter().writeValueAsString(object));
jste89
  • 426
  • 4
  • 15
0

Since you are using @ResponseBody annotation the response will be automatically converted to JSON. Make sure you include jackson mapper library in your class path.

If you are using Maven, you can add the jackson dependency as follows :

<dependency>  
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.8.2</version>
</dependency>

<dependency>  
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-core-asl</artifactId>
    <version>1.8.2</version>
</dependency>
codeMan
  • 5,730
  • 3
  • 27
  • 51
0

Use the RestController : org.springframework.web.bind.annotation.RestController to get automatic conversion of the responseBody into a JSON Format.

Ashoka
  • 935
  • 7
  • 20