I have a CREST REST application that responded in XML, however, I want to change it and get it to respond to JSON.
I can do the GET in the restclient, but the POST, PUT and DELETE do not, it gives the error 415 Unsupported Media Type. What else should I include in my code for it to work?
Follow the code below:
package pojo;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Aluno {
private int matricula;
private String nome;
private Endereco endereco;
private Curso curso;
public Aluno() {
}
public Aluno(int matricula, String nome, Endereco endereco, Curso curso) {
super();
if (!nome.isEmpty())
{
this.matricula = matricula;
this.nome = nome;
this.endereco = endereco;
this.curso = curso;
}
}
public int getMatricula() {
return matricula;
}
public void setMatricula(int matricula) {
this.matricula = matricula;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public Endereco getEndereco() {
return endereco;
}
public void setEndereco(Endereco endereco) {
this.endereco = endereco;
}
public Curso getCurso() {
return curso;
}
public void setCurso(Curso curso) {
this.curso = curso;
}
}
My CourseResource:
package resources;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
//import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import com.google.gson.Gson;
import pojo.Curso;
import client.EscolaService;
@Path("curso")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class CursoResource {
private EscolaService escolaService;
public CursoResource() {
this.escolaService = new EscolaService();
}
@GET
@Path("{codigo}")
public Response buscarCurso(@PathParam("codigo") String codigo) {
Curso curso = escolaService.buscarCurso(new Integer(codigo).intValue());
if (curso == null) {
return Response.status(HttpServletResponse.SC_NOT_FOUND).build();
}
System.out.println(new Gson().toJson(curso));
Response resposta = Response.ok(new Gson().toJson(curso)).build();
return resposta;// coloquei porque estava dando erro
}
@POST
@Consumes("application/json")
public Response cadastrarCurso(Curso curso) {
if ((curso.getCodigo() > 0) && (curso.getCodigo() < 100))
{
curso = escolaService.cadastrarCurso(curso);
try {
return Response.created(new URI("" + curso.getCodigo())).build();
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
Response resposta = Response.ok(new Gson().toJson(curso)).build();
return resposta.noContent().build();
}
@PUT
public Response alterarCurso(Curso curso) {
curso = escolaService.alterarCurso(curso);
try {
return Response.created(new URI("" + curso.getCodigo())).build();
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
@DELETE
@Path("{codigo}")
public Response removerCurso(@PathParam("codigo") String codigo) {
boolean excluir = escolaService.removerCurso(new Integer(codigo)
.intValue());
if (excluir == false) {
return Response.status(HttpServletResponse.SC_NOT_FOUND).build();
}
Response resposta = Response.ok().build();
return resposta;
}
@GET
@Produces("text/plain")
public String listarCursos() {
List<Integer> codigos = new ArrayList<Integer>();
List<String> nomes = new ArrayList<String>();
for (Iterator<Curso> it = escolaService.listarCursos().iterator(); it
.hasNext();) {
Curso curso = (Curso) it.next();
codigos.add(curso.getCodigo());
nomes.add(curso.getNome());
}
String separador = System.getProperty("line.separator");
return ("Cursos" +separador+ separador+ "Codigos: " + codigos.toString() +separador+ "Nomes: " + nomes.toString());
}
}