0

Maybe the title is not the best to explain the problem, but I'll try to explain it as best as possible.

I have an Spring Boot Application using JPA and MySQL, so in order to check everything worked properly, I made a simple CRUD test for my database, and I found problems with autowiring which are explained in my previous question. The solution for those problems was just adding the @ComponentScan annotation to my Application.java.

It was the solution for the test because it run without problems, but then I find another problem. Apart from the test, I need my application to show a list of Proposals made by some Users and also some Comments. Before adding that annotation, the HTMLs showed the correct information, but after adding it shows information about the database in JSON format on the main page and if I try to navigate to "localhost:8080/viewProposal" p.e. it shows a WhiteLabel error page with error code 404. I have no idea why it is replacing the HTMLs because I have just one controller and is not a RESTController. These are my classes:

Application.java

@SpringBootApplication
@EntityScan("persistence.model")
@EnableJpaRepositories("persistence.repositories")
@ComponentScan("services.impl")
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

MainController.java

@Controller
@RequestMapping("/")
public class MainController {

    private static final Logger logger = Logger.getLogger(MainController.class);
    private List<SseEmitter> sseEmitters = Collections.synchronizedList(new ArrayList<>());


    private Map<String, Proposal> proposals = generateProposals();

    @RequestMapping({"/live","/"})
    public String landing(Model model) {
        return "index";
    }

    @RequestMapping("/viewProposal")
    public String viewProposal(Model model, Long id) {
        //put the object in the map
        return "viewProposal";
    }

    @KafkaListener(topics = "newVote")
    public void listen(String data) {
        String[] contents = data.split(";");

        if(contents.length!=2)
            return;

        Proposal p;
        int newVote;

        if (proposals.containsKey(contents[0]))
            p = proposals.get(contents[0]);
        else {
            p = new Proposal();
            p.setTitle(contents[0]);
            proposals.put(p.getTitle(), p);
        }

        if (contents[1].equals("+"))
            newVote = +1;
        else if (contents[1].equals("-"))
            newVote = -1;
        else
            newVote = 0;

        p.setNumberOfVotes(p.getNumberOfVotes() + newVote);

        logger.info("New message received: \"" + data + "\"");
    }

    private static Map<String, Proposal> generateProposals() {
        Map<String, Proposal> lista = new HashMap<String, Proposal>();

        Proposal p = new Proposal();
        p.setTitle("tituloPrueba");

        lista.put("tituloPrueba", p);

        return lista;
    }

    @ModelAttribute("proposals")
    public Map<String, Proposal> getProposals() {
        return proposals;
    }

    public void setProposals(Map<String, Proposal> proposals) {
        this.proposals = proposals;
    }

}

MvcConfiguration

@Configuration
@EnableWebMvc
public class MvcConfiguration extends WebMvcConfigurerAdapter{
    @Bean
    public ViewResolver getViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/resources/templates/");
        resolver.setSuffix(".html");
        return resolver;
    }

    @Override
    public void configureDefaultServletHandling(
        DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }    

    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        configurer.defaultContentType(MediaType.TEXT_HTML);
    }    
}

If you want to see the rest of the classes, please go to my previous question everything is in there.

Thanks in advance.

Community
  • 1
  • 1
SergioMD15
  • 77
  • 1
  • 7

0 Answers0