HTTP status 500 Internal server error

Asked

Viewed 3,880 times

0

I have a problem in my web application in spring boot compiles and starts the server, but when it starts localhost:8080/buscar appears the following error:

inserir a descrição da imagem aqui Controller class:

package com.web.app.Bresource;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.web.app.Cservice.Service;
import com.web.app.Emodel.Bicicleta;

/* Controlador*/
@org.springframework.stereotype.Controller
@RestController
public class Controller {

    @Autowired
    private Service Service;

        @RequestMapping("/buscar") //buscar todos
        public List<Bicicleta> getTodos() {
            return Service.getTodos();
        }

        @RequestMapping("/buscar/{id}")
        public Bicicleta getCodigo(@PathVariable int id) {
            return Service.getCodigo(id); //buscar por codigo
        }

        //metodo post
        @RequestMapping(method=RequestMethod.POST, value="/inserir")
        public void addDados(@RequestBody Bicicleta inserir) {
            Service.addDados(inserir);
        }

        //metodo put
        @RequestMapping(method=RequestMethod.PUT, value="/atualizar/{id}")
        public void atualizarDados(@RequestBody Bicicleta atualizar, @PathVariable String id) {
            Service.atualizarDados(id, atualizar);
        }

        //metodo delete
        @RequestMapping(method=RequestMethod.DELETE, value="/delete/{id}")
        public Bicicleta deleteDados(@PathVariable String id) {
            return Service.deleteDados(id);
        }

        //colocando dados
        @RequestMapping(method = RequestMethod.POST)
        public String dados() {
            List<Bicicleta> bicicleta = new ArrayList<Bicicleta>();
            Bicicleta nova = new Bicicleta(0, "Kaloi", "doze", "monarca", 2, "kaloi");
            bicicleta.add(nova);

            return "Dados no sistema";
        }

}

Service class:

package com.web.app.Cservice;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import com.web.app.Drepository.TopicRepository;
import com.web.app.Emodel.Bicicleta;
import com.web.app.Bresource.Controller;

@org.springframework.stereotype.Service
public class Service {

    @Autowired
    private TopicRepository repository; 

    //buscar todos
    public static List<Bicicleta> getTodos(){
        return getTodos();
    }

    //buscar por codigo
    public Bicicleta getCodigo(int id) {
        return getCodigo(id);
    }

    //adicionar
    public void addDados(Bicicleta inserir) {
        ((List<Bicicleta>) inserir).add(inserir);
    }

    public void atualizarDados(String marca, Bicicleta atualizar) {
        Bicicleta t = new Bicicleta();
        if(t.getMarca().equals(marca)) {
            atualizar.setMarca(marca);;
            return;
        }
    }

    //metodo para criar o delete
    public Bicicleta deleteDados(String id) {
        if(id.equals(id)) {
            deleteDados(id);
        }

        return deleteDados(id);
    }

}

Someone’s been through it could help me?

  • The mistake is quite clear, since it is a StackOverflowError. Basically your methods in Service are calling themselves recursively without stopping criteria.

3 answers

1

I made some modifications and I succeeded, I put it like this:

Controller:

@RequestMapping(value = "/buscar", method = RequestMethod.GET) //buscar todos
public String list(Model model) {
    model.addAttribute("Bicicletas", Service.getTodos());
    return "Bicicletas";
}

Service:

@Service
public class ServiceImplements implements com.web.app.Cservice.Service {

    @Autowired
    private TopicRepository topicrepository;

    public Iterable<Bicicleta> getTodos() {
        return topicrepository.findAll();
    }

Topicrepository:

import org.springframework.data.repository.CrudRepository;
import com.web.app.Emodel.Bicicleta;

public interface TopicRepository extends CrudRepository<Bicicleta, Long> {

}

It became easier to understand by dismembering, for me it was also easier to understand the service class and the Repository, I created an interface instead of the class.

1

Dude, there’s some weird stuff in your code. Come on:

  1. In the method getTodos() class Controller, you are calling the object Service with the same class name. This is not only wrong, it can cause confusion. Change to:

    @Autowired
    private Service service;
    
  2. In class Service, you realize the @Autowired in a class object TopicRepository, but does not use it in any method. I believe that the method getTodos() is returning your own instance instead of the list of all bikes. I think it would be something like return repository.findAll()

I recommend you use a different nomenclature instead of the names of the Annotations:

public class Controller => public class BicicletaController

public class Service => public class BicicletaService

0


I recommend also checking when the server goes up, check if the "/search" method is being registered as GET, because in its initial declaration only @Requestmapping was placed (missing the method attribute)

In Spring 4.3 you can already directly use the @Getmapping annotation (as well as the other http verbs)

Browser other questions tagged

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