HTTP status 500 internal server error

0

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

Classcontroller:

packagecom.web.app.Bresource;importjava.util.ArrayList;importjava.util.List;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.web.bind.annotation.PathVariable;importorg.springframework.web.bind.annotation.RequestBody;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestMethod;importorg.springframework.web.bind.annotation.RestController;importcom.web.app.Cservice.Service;importcom.web.app.Emodel.Bicicleta;/*Controlador*/@org.springframework.stereotype.Controller@RestControllerpublicclassController{@AutowiredprivateServiceService;@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";
        }

}

Class service:

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);
    }

}

Has anyone gone through this could you help me?

    
asked by anonymous 08.10.2017 / 09:42

3 answers

0

I recommend checking also when to upload the server, check if the "/ fetch" method is being registered as GET, since in its initial declaration only the @RequestMapping (method attribute missing)

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

    
17.10.2017 / 21:46
1

Dude, there are some strange things in your code. Come on:

  • In the getTodos() method of the Controller class, you are calling the Service object with the same class name. This is not only wrong, but also confusing. Change to:

    @Autowired
    private Service service;
    
  • In class Service , you perform @Autowired on an object of class TopicRepository , but do not use it in any method. I believe the getTodos() method is returning an instance of itself instead of the list of all bicycles. I think it would be something like return repository.findAll()

  • I recommend you use a different nomenclature instead of annotation names:

    public class Controller = > public class BicicletaController

    public class Service = > public class BicicletaService

        
    08.10.2017 / 18:34
    1

    I made some changes and I was successful, 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 was easier to understand dismembering, for me it was easier to understand the service class and the repository, I created an interface instead of the class.

        
    09.10.2017 / 05:27