Delete Oracle - Api Web

1

I'm trying to make this springboot code, turn a code into 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; Home 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;
}

}

    
asked by anonymous 26.05.2018 / 19:19

1 answer

0

Your method of deleting has the following signature:

public String deletarEvento(long codigo)

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

@RequestMapping("/deletarEvento")

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

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

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

http://blablabla/deletarEvento/1

To get into this, try changing your method to this here:

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

In this way, we're telling Spring to feed the variable codigo only with the come within {id} . Now, he will know how to handle that URL that we expect.

Just be careful of one thing. The way you've done your logic, the delete() method expects a Evento , but what if findById() does not find Evento ? Yes, you will take NullPointerException when you try to delete. Think about it and work your logic to predict cases where an event is not found in the database;)

Finally, check Spring Data, especially how to make Spring create CRUD methods for you. Start by here .

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

    
28.05.2018 / 23:25