I am currently working on a small personal project(springboot + MYSQL), in that while testing in POSTMAN , its showing 404 resource not found .
NOTE: both application and MYSQL is running successfully without any problem and POSTMAN is working properly.
controller is fine as of my knowledge.
where is the problem in code? kindly help me out.
Main class
@SpringBootApplication
public class CafeManagementSystemApplication {
public static void main(String[] args) {
SpringApplication.run(CafeManagementSystemApplication.class, args);
}
}
controller
INTERFACE :
@RequestMapping(path = "/user")
public interface UserRest {
@PostMapping(path = "/signup")
public ResponseEntity<String> signup(@RequestBody(required = true) Map<String,String> requestMap);
}
CLASS:
@RestController
@RequestMapping(path = "/user")
public class UserRestImpl implements UserRest {
@Autowired
UserService userService;
@PostMapping(path = "/signup")
public ResponseEntity<String> signup(Map<String, String> requestMap) {
// TODO Auto-generated method stub
try {
return userService.signUp(requestMap);
} catch (Exception e) {
e.printStackTrace();
}
return CafeUtils.getResponseEntity(CafeConstants.SOMETHING_WENT_WRONG, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
Service
@Service
@Slf4j
public class UserServiceImpl implements UserService{
@Autowired
UserDao userDao; // repository
@Override
public ResponseEntity<String> signUp(Map<String, String> requestMap) {
try {
log.info("{Inside signUp {}",requestMap);
if(validateSignupMap(requestMap)) { // method present in same class
User user=userDao.findByEmailId(requestMap.get("email"));
if(Objects.isNull(user)) {
userDao.save(getUserFromMap(requestMap));
return CafeUtils.getResponseEntity("Successfully registered ", HttpStatus.OK);
}
else {
return CafeUtils.getResponseEntity("email already present",HttpStatus.BAD_REQUEST);
}
}
else {
return CafeUtils.getResponseEntity(CafeConstants.INVALID_DATA, HttpStatus.BAD_REQUEST);
}
}
catch(Exception e) {
e.printStackTrace();
}
return CafeUtils.getResponseEntity(CafeConstants.SOMETHING_WENT_WRONG, HttpStatus.INTERNAL_SERVER_ERROR);
}
private Boolean validateSignupMap(Map<String,String> requestMap) {
if(requestMap.containsKey("name") && requestMap.containsKey("contactNumber") && requestMap.containsKey("email") && requestMap.containsKey("password"))
{
return true;
}
return false;
}
private User getUserFromMap(Map<String,String> map) { // MAP to USER object
User u= new User();
u.setName(map.get("name"));
u.setContactNumber(map.get("contactNumber"));
u.setEmail(map.get("email"));
u.setPassword(map.get("password"));
u.setStatus("false");
u.setRole("user");
return u;
}
}
POJO
@NamedQuery(name="User.findByEmailId", query = "select u from User u where u.email=:email")
@Entity
@Table(name="user")
@DynamicInsert
@DynamicUpdate
@Data
public class User implements Serializable{
private static final long serialVersionUID=1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(name = "name")
private String name;
@Column(name = "conatactNumber")
private String contactNumber;
@Column(name = "email")
private String email;
@Column(name = "password")
private String password;
@Column(name = "status")
private String status;
@Column(name = "role")
private String role;
}
**REPO **
public interface UserDao extends JpaRepository<User, Integer> {
User findByEmailId(@Param("email") String email);
}
application.properties
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/cafe?allowPublicKeyRetrieval=true&useSSL=false
spring.datasource.username=root
spring.datasource.password=password_root
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jpa.properties.hibernate.format_sql=true
server.port=8081
POM.XML
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.inn.cafe</groupId>
<artifactId>com.inn.cafe</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Cafe Management System</name>
<description>Cafe Management System Project </description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
I tried to test in POSTMAN ,but its showing 404 error .
NOTE: postman is working properly ,i have checked by testing another application.
what the problem in the code ? where to look out for in the application
when i hit http://localhost:8081/user/signup , i am getting 404
Can you also share your application.properties/yml file. You may have set server.servlet.context-path=/myApp something like this and may have forgotten. Or gave a different port number with server.port=8082. All and all, sharing your configuration may give more insight about what to look for.
As for the comments about selecting proper Http method; In that case the server would have returned 405: Method Not Allowed. 404 means it couldn't find the requested resource.
Edit:
After copy pasting everything, I ran the application and I was definitely able to hit the url. PostmanScreenshot.
Something is wrong with your postman maybe? You said postman is working fine but maybe you had a setting(something like a proxy config) there for a specific application.
Can you add these two dependencies and see if you can hit from swagger.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.0.4</version>
</dependency>
http://localhost:8081/swagger-ui/index.html
I replaced
CafeUtils.getResponseEntity(CafeConstants.INVALID DATA, HttpStatus.BAD_REQUEST);
to
new ResponseEntity<>("INVALID DATA", HttpStatus.BAD_REQUEST);
everything else is the same as yours.