Создание первой конечной точки REST
Создайте простую конечную точку REST «Hello World» и разберитесь в основных аннотациях контроллеров.
«Создание первой конечной точки REST» — бесплатный урок Spring Boot 4 Microservices & REST APIs на CoddyKit. Это урок 2 из 3. Ты можешь прочитать весь урок бесплатно ниже — а потом практиковать его прямо в браузере с встроенным редактором кода и ИИ-репетитором 24/7. Это часть пути обучения Spring Boot 4 Microservices & REST APIs, и твой прогресс синхронизируется между веб-версией и приложением CoddyKit. Курс Spring Boot 4 Microservices & REST APIs содержит 3 уроков всего.
Части этого урока еще не переведены и отображаются на английском.
What's a REST Endpoint?
An endpoint is a specific URL where your app can be reached — a unique doorbell for one function. Our goal: build one that says “Hello!”
Spring Boot Makes REST Easy
Spring Boot is great for REST APIs: minimal setup, an embedded server like Tomcat, and convention over configuration — less boilerplate, more building.
Meet @RestController
@RestController marks a class to handle web requests and return data directly. It bundles @Controller with @ResponseBody — the core REST building block.
Code Your First Endpoint
Let’s code a Hello CoddyKit! endpoint at the root path /. Run it to see your first Spring Boot API respond.
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class FirstApiApplication {
public static void main(String[] args) {
SpringApplication.run(FirstApiApplication.class, args);
}
@GetMapping("/")
public String hello() {
return "Hello CoddyKit!";
}
}Decoding @GetMapping
@GetMapping("/") maps HTTP GET requests to a method, and the path in parentheses sets the URL. So a GET to / calls hello().
Running Your Application
On run, Spring Boot starts an embedded Tomcat on port 8080, scans for annotations like @RestController and @GetMapping, and registers your endpoints.
Test Your Endpoint!
Now test it: open http://localhost:8080/ in a browser (or curl it) and you’ll see “Hello CoddyKit!” — your first live endpoint.
Mapping Specific Paths
Want more endpoints? Just give each @GetMapping a different path. Here we add a second one at /greet.
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class FirstApiApplication {
public static void main(String[] args) {
SpringApplication.run(FirstApiApplication.class, args);
}
@GetMapping("/")
public String hello() {
return "Hello CoddyKit!";
}
@GetMapping("/greet")
public String greet() {
return "Greetings from Spring Boot!";
}
}Quick Check: Endpoint Basics
Which annotation tells Spring Boot that a class is a web controller and its methods should return data directly as the response body?
Recap: Your First API
Nice work! You built your first REST API — what an endpoint is, the @RestController annotation, @GetMapping for paths, and running and hitting your app.
Часто задаваемые вопросы
Урок «Создание первой конечной точки REST» бесплатный?
Да — полный текст урока «Создание первой конечной точки REST» бесплатно доступен здесь в веб-версии. Чтобы практиковать его интерактивно (встроенный редактор кода и ИИ-репетитор 24/7) и разблокировать остальной курс Spring Boot 4 Microservices & REST APIs, подпишись на CoddyKit PRO. Курс Spring Boot 4 Microservices & REST APIs содержит 3 уроков всего.
Чему я научусь в уроке «Создание первой конечной точки REST»?
Создайте простую конечную точку REST «Hello World» и разберитесь в основных аннотациях контроллеров. Ты практикуешь Spring Boot 4 Microservices & REST APIs с помощью реального кода, который запускаешь прямо в браузере, и ИИ-репетитор 24/7 отвечает на твои вопросы во время урока.
Нужен ли мне опыт, чтобы начать Spring Boot 4 Microservices & REST APIs?
Предыдущий опыт не требуется. Spring Boot 4 Microservices & REST APIs на CoddyKit структурирован для всех уровней — от новичков до продвинутых, поэтому ты можешь начать отсюда или с самого начала и учиться в своем темпе. Это урок 2 из 3.
Сколько времени занимает урок «Создание первой конечной точки REST»?
Большинство уроков CoddyKit занимают около 5–10 минут. Каждый из них компактный и интерактивный, поэтому ты постоянно делаешь прогресс и продолжаешь с того же места в веб-версии и приложении.
Можно ли писать и запускать код в этом уроке Spring Boot 4 Microservices & REST APIs?
Да. Каждый урок Spring Boot 4 Microservices & REST APIs включает встроенный редактор кода, поэтому ты пишешь и запускаешь реальный код прямо в браузере и получаешь моментальную обратную связь от AI — локальная установка не требуется.
Все уроки этого курса
- Настройка проекта Spring Boot
- Создание первой конечной точки REST
- Понимание методов и статусов HTTP