Delete Oracle - Web Api

Asked

Viewed 78 times

1

I’m trying to make this springboot code, become a code in Webapi

@RequestMapping("/deletarEvento")
    public String deletarEvento(long codigo)
{
        Evento evento = er.findByCodigo(codigo);
        er.delete(evento);
        return "redirect:/eventos";
}

More when I try to do

@RequestMapping(value="/deletarEvento", method=RequestMethod.DELETE)
    public String deletarEvento(long codigo)
{
        Evento evento = er.findByCodigo(codigo);
        er.delete(evento);
        return "redirect:/eventos";
}

he gives this error : There was an Unexpected error (type=Bad Request, status=400). Failed to Convert value of type 'java.lang.String' to required type 'long'; nested Exception is java.lang.NumberFormatException: For input string: "deletarEvento"


Event Class

package com.vestibulartio.models;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Generatedvalue;
import javax.persistence.Generationtype;
import javax.persistence.Id;
import javax.persistence.Onetomany;
import javax.persistence.Table;

@Entity
@Table(name="event")
public class Event{

@Id
@Column(name="codigo")
private Long codigo;


//@OneToMany
//private List<Convidado> convidado;

@Column(name="nome")
private String nome;
@Column(name="local")
private String local;
@Column(name="data")
private String data;
@Column(name="horario")
private String horario;

public Long getCodigo() {
    return codigo;
}
public void setCodigo(Long codigo) {
    this.codigo = codigo;
}
public String getNome() {
    return nome;
}
public void setNome(String nome) {
    this.nome = nome;
}
public String getLocal() {
    return local;
}
public void setLocal(String local) {
    this.local = local;
}
public String getData() {
    return data;
}
public void setData(String data) {
    this.data = data;
}
public String getHorario() {
    return horario;
}
public void setHorario(String horario) {
    this.horario = horario;
}

} package com.vestibulartio.models;

import java.util.List;

import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Generatedvalue; import javax.persistence.Generationtype; import javax.persistence.Id; import javax.persistence.Onetomany; import javax.persistence.Table;

@Entity @Table(name="event") public class Event{

@Id
@Column(name="codigo")
private Long codigo;


//@OneToMany
//private List<Convidado> convidado;

@Column(name="nome")
private String nome;
@Column(name="local")
private String local;
@Column(name="data")
private String data;
@Column(name="horario")
private String horario;

public Long getCodigo() {
    return codigo;
}
public void setCodigo(Long codigo) {
    this.codigo = codigo;
}
public String getNome() {
    return nome;
}
public void setNome(String nome) {
    this.nome = nome;
}
public String getLocal() {
    return local;
}
public void setLocal(String local) {
    this.local = local;
}
public String getData() {
    return data;
}
public void setData(String data) {
    this.data = data;
}
public String getHorario() {
    return horario;
}
public void setHorario(String horario) {
    this.horario = horario;
}

}

  • You can post the class Evento?

  • @Statelessdev placed

1 answer

0


Your delete method has the following signature:

public String deletarEvento(long codigo)

He expects a codigo, which is the id that you pass to the method findById(). So, in the URL that makes the call to the delete method, you should pass a code, however, see how your URL is mounted:

@RequestMapping("/deletarEvento")

Where is the codigo? For Spring, the code is everything that comes after the bar, in case, "deletarEvento", which is a String and which obviously cannot be converted to long, which is what the delete method waits for. This is the explanation of the error message you took:

java.lang.NumberFormatException: For input string: "deletarEvento"

The correct URL your method expects is something like this, with a number representing the Event code you want to delete:

http://blablabla/deletarEvento/1

To get to that, try changing your method to this:

@RequestMapping("/deletarEvento/{id}", method=RequestMethod.DELETE)
public String deletarEvento(@PathVariable("id") long codigo) {
   Evento evento = er.findByCodigo(codigo);
   er.delete(evento);
   return "redirect:/eventos";
}

So we’re telling Spring to feed the variable codigo just with the coming inside {id}. Now, he’ll know how to handle that URL we’re hoping for.

Just watch out for one thing. The way you made your logic, the method delete() expecting a Evento, but, what if the findById() not finding a Evento? Yes, you’ll have one NullPointerException when trying to delete. Think about it and work your logic to predict cases where an event is not found in the database ;)

Finally, get a look at Spring Data, especially how to get Spring to create the CRUD methods yourself. Start with here.

If that answer helped you, mark it as correct for others to use it as well.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.