controller
import com.example.apilearn.entity.Cat;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
//@RequestMapping("/api")
@RestController
public class MainController {
@Autowired
private ObjectMapper objectMapper;
@GetMapping("/api/main")
public String mainListener(){
return "Hello";
}
@GetMapping("/api/cat")
public String giveCat(){
Cat cat = new Cat("Barsik", 10, 7);
String jsonData = null;
try {
jsonData = objectMapper.writeValueAsString(cat);
} catch (JsonProcessingException e) {
System.out.println("Error");
}
return jsonData;
}
}
entity
@Getter
@Setter
//@AllArgsConstructor
//@NoArgsConstructor
public class Cat {
String name;
int age;
int weight;
public Cat(String name, int age, int weight) {
this.name = name;
this.age = age;
this.weight = weight;
}
public Cat() {
}
}
application.properties
server.port=8080
Main
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ApilearnApplication {
public static void main(String[] args) {
SpringApplication.run(ApilearnApplication.class, args);
}
}
при любом запросе (по локалхосту или IP) ловлю:
{
"timestamp": "2024-12-08T06:19:49.879+00:00",
"status": 404,
"error": "Not Found",
"path": "/api/cat"
}
Подскажите, пожалуйста, в чем может быть проблема?