Can't get Postman to give a 200 despite following freeCodeCamp tutorial on Springboot

61 Views Asked by At

I have followed a beginner guide from freeCodeCamp, and unlike the video, do not get a 200 status code instead a 404. I have searched stack overflow and other forums but nothing works. postman showing 404

I read that it is possible that in a spring boot application with multiple packages that the main activity needs to scan them with @ComponentScan annotation to check all packages. However that did not help. Other solutions I have tried and not worked are from postman (), or here. I also re-created this on a different computer but it still gives me a 404.

I also checked to make sure that postman is correctly configured, following the documentation guide, which was successful for the get: Get And the post: Post

I would greatly appreciate some insight into what are some possible solutions, or what am I doing wrong?

Edit Code blocks

User class:

import com.fasterxml.jackson.annotation.JsonProperty;
import org.springframework.boot.autoconfigure.domain.EntityScan;

import java.util.UUID;

@EntityScan
public class User {
    private final UUID id;
    private String name;

    public UUID getId() {
        return id;
    }


    public String getName() {
        return name;
    }

    public User(@JsonProperty("id") UUID id,
                @JsonProperty("name") String name) {
        this.id = id;
        this.name = name;
    }
}

Repository class: package repository;

import model.User;
import org.springframework.stereotype.Repository;

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

@Repository("dao")
public class UserRepositoryImpl implements UserRepository{
    private static List<User> db = new ArrayList<User>();

    @Override
    public List<User> getAll() {
        return db;
    }

    @Override
    public int insertUser(UUID id, User user) {
        db.add(new User(id, user.getName()));
        return 1;
    }
}

service class:

import model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import repository.UserRepositoryImpl;

import java.util.List;


@Service
public class UserService {

    private final UserRepositoryImpl userRepository;

    @Autowired
    public UserService(@Qualifier("fakedao") UserRepositoryImpl userRepository) {
        this.userRepository = userRepository;
    }

    public int addUser(User user){
        return userRepository.insertUser(user);

    }

    public List<User> getAll(){
        return userRepository.getAll();
    }
}

Class with main:

package com.example.demo;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class  DemoApplication {

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

}

Log:

[2m2023-06-25 12:01:51.102[0;39m [32m INFO[0;39m [35m19784[0;39m [2m---[0;39m [2m[           main][0;39m [36mcom.example.demo.DemoApplication        [0;39m [2m:[0;39m Starting DemoApplication using Java 18.0.2.1 on DESKTOP-QQ7JP27 with PID 19784 (C:\Users\__\Desktop\Workspace\Java Projects\demo\target\classes started by __ in C:\Users\__\Desktop\Workspace\Java Projects\demo)
[2m2023-06-25 12:01:51.123[0;39m [32m INFO[0;39m [35m19784[0;39m [2m---[0;39m [2m[           main][0;39m [36mcom.example.demo.DemoApplication        [0;39m [2m:[0;39m No active profile set, falling back to 1 default profile: "default"
[2m2023-06-25 12:01:52.463[0;39m [32m INFO[0;39m [35m19784[0;39m [2m---[0;39m [2m[           main][0;39m [36mo.s.b.w.embedded.tomcat.TomcatWebServer [0;39m [2m:[0;39m Tomcat initialized with port(s): 8080 (http)
[2m2023-06-25 12:01:52.479[0;39m [32m INFO[0;39m [35m19784[0;39m [2m---[0;39m [2m[           main][0;39m [36mo.apache.catalina.core.StandardService  [0;39m [2m:[0;39m Starting service [Tomcat]
[2m2023-06-25 12:01:52.479[0;39m [32m INFO[0;39m [35m19784[0;39m [2m---[0;39m [2m[           main][0;39m [36morg.apache.catalina.core.StandardEngine [0;39m [2m:[0;39m Starting Servlet engine: [Apache Tomcat/9.0.75]
[2m2023-06-25 12:01:52.613[0;39m [32m INFO[0;39m [35m19784[0;39m [2m---[0;39m [2m[           main][0;39m [36mo.a.c.c.C.[Tomcat].[localhost].[/]      [0;39m [2m:[0;39m Initializing Spring embedded WebApplicationContext
[2m2023-06-25 12:01:52.613[0;39m [32m INFO[0;39m [35m19784[0;39m [2m---[0;39m [2m[           main][0;39m [36mw.s.c.ServletWebServerApplicationContext[0;39m [2m:[0;39m Root WebApplicationContext: initialization completed in 1317 ms
[2m2023-06-25 12:01:53.012[0;39m [33m WARN[0;39m [35m19784[0;39m [2m---[0;39m [2m[           main][0;39m [36mion$DefaultTemplateResolverConfiguration[0;39m [2m:[0;39m Cannot find template location: classpath:/templates/ (please add some templates, check your Thymeleaf configuration, or set spring.thymeleaf.check-template-location=false)
[2m2023-06-25 12:01:53.118[0;39m [32m INFO[0;39m [35m19784[0;39m [2m---[0;39m [2m[           main][0;39m [36mo.s.b.w.embedded.tomcat.TomcatWebServer [0;39m [2m:[0;39m Tomcat started on port(s): 8080 (http) with context path ''
[2m2023-06-25 12:01:53.131[0;39m [32m INFO[0;39m [35m19784[0;39m [2m---[0;39m [2m[           main][0;39m [36mcom.example.demo.DemoApplication        [0;39m [2m:[0;39m Started DemoApplication in 2.653 seconds (JVM running for 6.289)

Links as stackoverflow is saying this spam: Video: https://www.youtube.com/watch?v=vtPkZShrvXQ&t=2141s&ab_channel=freeCodeCamp.org

Some of the solutions I have read and tried: https://support.postman.com/hc/en-us/articles/6235689752599-Fixing-a-404-Not-Found-error-response

Spring Boot Rest : Error 404 not found when posting JSON via Postman), or [here](Spring Boot GET Request gives 200 OK status, but Postman returns "ø" as response body

0

There are 0 best solutions below