I keep receiving 404 Not Found error for my MVC app. Here are my code:
application.properties
spring.datasource.url=jdbc:postgresql://localhost:5432/demo1
spring.datasource.username=demo1
spring.datasource.password=demo1
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.generate-ddl=true
spring.jpa.show-sql=true
server.port=5000
## Hibernate Properties
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto = update
server.servlet.context-path=/api/
spring.liquibase.change-log=classpath:db/changelog/master_changelog.xml
spring.liquibase.user=demo1
spring.liquibase.password=demo1
Person model
@Entity
@Table(name = "person")
@Data
@AllArgsConstructor
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.UUID)
@Column(name="tax_id", nullable = false)
private Long taxId;
@Column(name = "first_name", nullable = false)
private String firstName;
@Column(name = "last_name", nullable = false)
private String lastName;
private String fatherName;
private String motherName;
private LocalDate dob;
public Person() {}
PersonRepository
public interface PersonRepository extends JpaRepository<Person, Long> { }
PersonController
@RestController
@RequestMapping("/api")
public class PersonController {
private final PersonService personService;
@Autowired
private final PersonRepository personRepository;
public PersonController(PersonService personService, PersonRepository personRepository) {
this.personService = personService;
this.personRepository = personRepository;
}
// get all persons
@GetMapping("/persons")
public List<Person> getAllPersons() {
return this.personRepository.findAll();
}
After running the Spring app, I have this 404 error:
{
"timestamp": "2024-08-14T02:52:37.363+00:00",
"status": 404,
"error": "Not Found",
"path": "/api/persons"
}
Even though when I go to Postgres DB, I can run:
SELECT * FROM person;
I containerize this application using Docker-compose. Here is my docker-compose.yml file:
version: '3.1'
services:
app:
image: 'docker-demo1-image:latest'
build:
context: .
container_name: demo1
depends_on:
- db
environment:
- SPRING_DATASOURCE_URL=jdbc:postgresql://db:5432/demo1
- SPRING_DATASOURCE_USERNAME=demo1
- SPRING_DATASOURCE_PASSWORD=demo1
- SPRING_JPA_HIBERNATE_DDL_AUTO=update
db:
image: 'postgres:13.1-alpine'
restart: always
ports:
- "5432:5432"
container_name: db
environment:
- POSTGRES_USER=demo1
- POSTGRES_PASSWORD=demo1
2
Answers
As you have added
server.servlet.context-path=/api/
in your application.properties, so, you need to call your endpoint like this –http://localhost:8080/api/api/persons
.Otherwise, remove this
server.servlet.context-path=/api/
field and call the endpoint like thishttp://localhost:8080/api/persons
.It will work. See if this helps.
Error In Invalid URL
There are two way to solve this:
Remove
server.servlet.context-path=/api/
this line fromapplication.properties
. When we addserver.servlet.context-path=/api
this line inapplication.properties
then every API Start with this prefix.OR
Remove
@RequestMapping("/api")
from PersonController.There is another problem in your controller.You use
@Autowired
incorrectly as you inject dependency by constructor there is no need of this annotation.