{"id":124031,"date":"2024-06-27T12:12:00","date_gmt":"2024-06-27T09:12:00","guid":{"rendered":"https:\/\/www.javacodegeeks.com\/?p=124031"},"modified":"2024-06-27T11:22:26","modified_gmt":"2024-06-27T08:22:26","slug":"spring-boot-mvc-rest-controller-example-unit-tests","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/spring-boot-mvc-rest-controller-example-unit-tests.html","title":{"rendered":"Spring Boot MVC REST Controller Example &amp; Unit Tests"},"content":{"rendered":"<p>In modern web development, REST APIs are essential for enabling communication between different systems and platforms. The Spring Framework, particularly Spring Boot, provides robust support for building RESTful web services, simplifying the development process through various annotations and tools. One such powerful annotation is <code>@RestController<\/code>, which is used in Spring MVC to create RESTful API controllers. Let us delve into understanding a Spring Boot REST controller example to illustrate how to build a RESTful web service using Spring Boot.<\/p>\n<h2><a name=\"introduction\"><\/a>1. What is @RestController annotation?<\/h2>\n<p>The <code>@RestController<\/code> annotation in Spring Boot is a specialized version of the <code>@Controller<\/code> annotation. It is typically used in web applications to handle RESTful web services. When a class is annotated with <code>@RestController<\/code>, it serves as a convenience annotation that combines <code>@Controller<\/code> and <code>@ResponseBody<\/code>. This means that the methods in the class will return data directly in the response body, rather than rendering a view. It is particularly useful for creating RESTful APIs, as it simplifies the development process by eliminating the need for explicit <code>@ResponseBody<\/code> annotations on each method. Overall, <code>@RestController<\/code> streamlines the development of RESTful web services by providing a clear and concise way to define controllers that return JSON or XML responses.<\/p>\n<h2><a name=\"setting-up-a-database\"><\/a>2. Setting up a database on Docker<\/h2>\n<p>Usually, setting up the database is a tedious step but with <a href=\"https:\/\/www.docker.com\/\" target=\"_blank\" rel=\"noopener\">Docker<\/a>, it is a simple process. You can watch the video available at this <a href=\"https:\/\/www.youtube.com\/watch?v=S7NVloq0EBc\" target=\"_blank\" rel=\"noopener\">link<\/a> to understand the Docker installation on Windows OS.  Once done open the terminal and trigger the below command to set and run postgresql.<\/p>\n<pre class=\"brush:plain; wrap-lines:false;\">\n-- Remember to change the password --\ndocker run -d -p 5432:5432 -e POSTGRES_PASSWORD= --name postgres postgres\n\n-- command to stop the Postgres docker container --\ndocker stop postgres\n\n-- command to remove the Postgres docker container --\ndocker rm postgres\n<\/pre>\n<p>Remember to enter the password of your choice. If everything goes well the postgresql database server will be up and running on a port number \u2013 <code>5432<\/code> and you can connect with the <a href=\"https:\/\/dbeaver.io\/\" target=\"_blank\" rel=\"noopener\">Dbeaver<\/a> GUI tool for connecting to the server.<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2024\/06\/postgressetupondocker-3.jpg\"><img decoding=\"async\" width=\"818\" height=\"133\" src=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2024\/06\/postgressetupondocker-3.jpg\" alt=\"\" class=\"wp-image-124032\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2024\/06\/postgressetupondocker-3.jpg 818w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2024\/06\/postgressetupondocker-3-300x49.jpg 300w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2024\/06\/postgressetupondocker-3-768x125.jpg 768w\" sizes=\"(max-width: 818px) 100vw, 818px\" \/><\/a><figcaption class=\"wp-element-caption\">Fig. 1. Postgres on Docker<\/figcaption><\/figure>\n<\/div>\n<h2>2.1 Setting up pre-requisite data<\/h2>\n<p>To proceed further with the tutorial we will set up the required mock data in the postgresql.<\/p>\n<pre class=\"brush:sql; wrap-lines:false;\">\ndrop database mydatabase;\n\ncreate database mydatabase;\n\ndrop table books;\n\ncreate table books (\n    id bigserial primary key,\n    title varchar(255) not null,\n    author varchar(255) not null,\n    price double precision not null\n);\n\ninsert into books (title, author, price) values ('the great gatsby', 'f. scott fitzgerald', 10.99);\ninsert into books (title, author, price) values ('1984', 'george orwell', 8.99);\ninsert into books (title, author, price) values ('to kill a mockingbird', 'harper lee', 12.50);\ninsert into books (title, author, price) values ('pride and prejudice', 'jane austen', 7.95);\ninsert into books (title, author, price) values ('the catcher in the rye', 'j.d. salinger', 9.99);\n\nselect * from books;\n<\/pre>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2024\/06\/postgresondockerbooksmockdata.jpg\"><img decoding=\"async\" width=\"506\" height=\"167\" src=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2024\/06\/postgresondockerbooksmockdata.jpg\" alt=\"\" class=\"wp-image-124033\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2024\/06\/postgresondockerbooksmockdata.jpg 506w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2024\/06\/postgresondockerbooksmockdata-300x99.jpg 300w\" sizes=\"(max-width: 506px) 100vw, 506px\" \/><\/a><figcaption class=\"wp-element-caption\">Fig. 2: Sample data<\/figcaption><\/figure>\n<\/div>\n<h2><a name=\"code-example\"><\/a>3. Code Example<\/h2>\n<h3>3.1 Dependencies<\/h3>\n<p>Add the following dependencies to your <code>build.gradle<\/code> file or if you have created a spring project from <a href=\"https:\/\/start.spring.io\/\" target=\"_blank\" rel=\"noopener\">start.spring.io<\/a> this won\u2019t be necessary as the file will be automatically populated with the dependencies information.<\/p>\n<pre class=\"brush:plain; wrap-lines:false;\">plugins {\n    id 'java'\n    id 'org.springframework.boot' version '3.3.0'\n    id 'io.spring.dependency-management' version '1.1.5'\n}\n\ngroup = 'jcg'\nversion = '0.0.1-SNAPSHOT'\n\njava {\n    toolchain {\n        languageVersion = JavaLanguageVersion.of(17)\n    }\n}\n\nrepositories {\n    mavenCentral()\n}\n\ndependencies {\n    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'\n    implementation 'org.springframework.boot:spring-boot-starter-validation'\n    implementation 'org.springframework.boot:spring-boot-starter-web'\n    runtimeOnly 'org.postgresql:postgresql'\n    testImplementation 'org.springframework.boot:spring-boot-starter-test'\n    testRuntimeOnly 'org.junit.platform:junit-platform-launcher'\n}\n\ntasks.named('test') {\n    useJUnitPlatform()\n}\n<\/pre>\n<h3>3.2 Configure application and database properties<\/h3>\n<p>Add the following properties to the <code>application.properties<\/code> file present in the <code>resources<\/code> folder.<\/p>\n<pre class=\"brush:plain; wrap-lines:false;\"># application name\nspring.application.name=springjpademo\n\n# database properties\nspring.datasource.url=jdbc:postgresql:\/\/localhost:5432\/mydatabase\nspring.datasource.username=postgres\nspring.datasource.password=somepostgrespassword\n\n# application properties\nserver.port=9090\nspring.main.banner-mode=off\nspring.main.log-startup-info=false\n<\/pre>\n<p>The properties file defines:<\/p>\n<ul>\n<li>Application Name:\n<ul>\n<li><code>spring.application.name=springjpademo<\/code> &#8211; Sets the name of the Spring application to &#8220;springjpademo&#8221;.<\/li>\n<\/ul>\n<\/li>\n<li>Database Properties:\n<ul>\n<li><code>spring.datasource.url=jdbc:postgresql:\/\/localhost:5432\/mydatabase<\/code> &#8211; Configures the JDBC URL for connecting to a PostgreSQL database named &#8220;mydatabase&#8221; running on localhost.<\/li>\n<li><code>spring.datasource.username=postgres<\/code> &#8211; Specifies the username for connecting to the PostgreSQL database.<\/li>\n<li><code>spring.datasource.password=somepostgrespassword<\/code> &#8211; Specifies the password associated with the username for database authentication.<\/li>\n<\/ul>\n<\/li>\n<li>Application Properties:\n<ul>\n<li><code>server.port=9090<\/code> &#8211; Sets the port number on which the Spring Boot application will run.<\/li>\n<li><code>spring.main.banner-mode=off<\/code> &#8211; Disables the startup banner that is normally displayed when the application starts.<\/li>\n<li><code>spring.main.log-startup-info=false<\/code> &#8211; Prevents logging of startup information such as application configuration details.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>3.3 Creating the Model classes<\/h3>\n<p>Create a Book entity class to interact with the <code>JpaRepository<\/code> interface and perform the CRUD operations.<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<pre class=\"brush:java; wrap-lines:false;\">@Entity\n@Table(name = \"books\")\npublic class Book {\n    @Id\n    @GeneratedValue(strategy = GenerationType.AUTO)\n    private Long id;\n\n    @NotEmpty(message = \"Title is required\")\n    private String title;\n\n    @NotEmpty(message = \"Author is required\")\n    private String author;\n\n    @NotNull(message = \"Price is required\")\n    private Double price;\n\n    \/\/ toString, Getters, and Setters methods\n}\n<\/pre>\n<p>We&#8217;ve added some basic validation to our <code>Book<\/code> entity using annotations like <code>@NotEmpty<\/code> and <code>@NotNull<\/code>.<\/p>\n<h3>3.4 Creating the Data interaction layer<\/h3>\n<p>Spring Data JPA builds on top of JPA and provides an interface, <code>JpaRepository<\/code>, that offers a range of out-of-the-box CRUD operations and the ability to define custom queries. By extending this interface, developers can create repositories for their domain entities without having to write detailed data access codes. This approach not only accelerates development but also ensures cleaner and more maintainable code.<\/p>\n<p>Create the book repository interface to interact with the book entity for interacting with the SQL table via the <code>JpaRepository<\/code> interface and perform the CRUD operations.<\/p>\n<pre class=\"brush:java; wrap-lines:false;\">@Repository\npublic interface BookRepository extends JpaRepository&lt;Book, Long&gt; {\n}\n<\/pre>\n<p>The <code>@Repository<\/code> annotation marks the <code>BookRepository<\/code> interface as a Spring Data repository, which is a specialized component used for data access and management. This interface extends <code>JpaRepository&lt;Book, Long&gt;<\/code>, where <code>Book<\/code> is the entity type that this repository manages, and <code>Long<\/code> is the type of the entity&#8217;s primary key. By extending <code>JpaRepository<\/code>, the <code>BookRepository<\/code> interface inherits a variety of methods for performing common database operations such as saving, finding, and deleting entities. This setup allows you to interact with the database without having to write boilerplate code for these operations. The <code>BookRepository<\/code> interface will be automatically implemented by Spring Data JPA, providing the necessary data access logic at runtime.<\/p>\n<h3>3.5 Create the Controller file<\/h3>\n<p>Now, let&#8217;s create a REST controller to handle CRUD operations for our <code>Book<\/code> resource:<\/p>\n<pre class=\"brush:java; wrap-lines:false;\">\/\/ Note- Skipping the service layer for brevity.\n@RestController\n@RequestMapping(\"\/api\/books\")\npublic class BookController {\n\n    @Autowired\n    private BookRepository bookRepository;\n\n    @GetMapping\n    public List&lt;Book&gt; getAllBooks() {\n        return bookRepository.findAll();\n    }\n\n    @GetMapping(\"\/{id}\")\n    public ResponseEntity&lt;Book&gt; getBookById(@PathVariable Long id) {\n        Optional&lt;Book&gt; book = bookRepository.findById(id);\n        return book.map(ResponseEntity::ok).orElseGet(() -&gt; ResponseEntity.notFound().build());\n    }\n\n    @PostMapping\n    public Book createBook(@Valid @RequestBody Book book) {\n        return bookRepository.save(book);\n    }\n\n    @PutMapping(\"\/{id}\")\n    public ResponseEntity&lt;Book&gt; updateBook(@PathVariable Long id, @Valid @RequestBody Book bookDetails) {\n        Optional&lt;Book&gt; book = bookRepository.findById(id);\n        if (book.isPresent()) {\n            Book existingBook = book.get();\n            existingBook.setTitle(bookDetails.getTitle());\n            existingBook.setAuthor(bookDetails.getAuthor());\n            existingBook.setPrice(bookDetails.getPrice());\n            return ResponseEntity.ok(bookRepository.save(existingBook));\n        } else {\n            return ResponseEntity.notFound().build();\n        }\n    }\n\n    @DeleteMapping(\"\/{id}\")\n    public ResponseEntity&lt;Book&gt; deleteBook(@PathVariable Long id) {\n        Optional&lt;Book&gt; book = bookRepository.findById(id);\n        if (book.isPresent()) {\n            bookRepository.delete(book.get());\n            return ResponseEntity.noContent().build();\n        } else {\n            return ResponseEntity.notFound().build();\n        }\n    }\n}\n<\/pre>\n<p>The code defines:<\/p>\n<ul>\n<li>@RestController: Marks the <code>BookController<\/code> class as a RESTful controller in a Spring application, allowing it to handle HTTP requests.<\/li>\n<li>@RequestMapping(&#8220;\/api\/books&#8221;): Sets the base URL path for all endpoints in this controller to <code>\/api\/books<\/code>.<\/li>\n<li>@Autowired private BookRepository bookRepository: Autowires (injects) an instance of <code>BookRepository<\/code> into the controller, enabling data access operations.<\/li>\n<li>@GetMapping: Handles HTTP GET requests to retrieve all books from the database.\n<ul>\n<li>Returns a list of all books by calling <code>bookRepository.findAll()<\/code>.<\/li>\n<\/ul>\n<\/li>\n<li>@GetMapping(&#8220;\/{id}&#8221;): Handles HTTP GET requests to retrieve a book by its ID.\n<ul>\n<li>Uses <code>@PathVariable Long id<\/code> to capture the book ID from the URL.<\/li>\n<li>Attempts to find the book using <code>bookRepository.findById(id)<\/code>.<\/li>\n<li>Returns <code>ResponseEntity.ok(book)<\/code> if the book is found, or <code>ResponseEntity.notFound().build()<\/code> if not.<\/li>\n<\/ul>\n<\/li>\n<li>@PostMapping: Handles HTTP POST requests to create a new book.\n<ul>\n<li>Validates the incoming request body (<code>@Valid @RequestBody Book book<\/code>) against validation rules defined in the <code>Book<\/code> class.<\/li>\n<li>Saves the new book to the database using <code>bookRepository.save(book)<\/code>.<\/li>\n<li>Returns the saved book object.<\/li>\n<\/ul>\n<\/li>\n<li>@PutMapping(&#8220;\/{id}&#8221;): Handles HTTP PUT requests to update an existing book.\n<ul>\n<li>Similar to <code>@GetMapping(\"\/{id}\")<\/code>, it captures the book ID and attempts to find the existing book.<\/li>\n<li>If the book exists, updates its details with those from <code>@Valid @RequestBody Book bookDetails<\/code>.<\/li>\n<li>Returns <code>ResponseEntity.ok(bookRepository.save(existingBook))<\/code> with the updated book if successful, or <code>ResponseEntity.notFound().build()<\/code> if the book does not exist.<\/li>\n<\/ul>\n<\/li>\n<li>@DeleteMapping(&#8220;\/{id}&#8221;): Handles HTTP DELETE requests to delete a book by its ID.\n<ul>\n<li>Attempts to find the book by ID and deletes it using <code>bookRepository.delete(book.get())<\/code> if found.<\/li>\n<li>Returns <code>ResponseEntity.noContent().build()<\/code> indicating successful deletion, or <code>ResponseEntity.notFound().build()<\/code> if the book does not exist.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>3.6 Create the Error handling file<\/h3>\n<p>To handle errors gracefully, we&#8217;ll create a global exception handler:<\/p>\n<pre class=\"brush:java; wrap-lines:false;\">\/\/ To handle errors gracefully we are creating this global exception handler\n@RestControllerAdvice\npublic class GlobalExceptionHandler {\n\n    @ExceptionHandler(MethodArgumentNotValidException.class)\n    public ResponseEntity&lt;Map&lt;String, String&gt;&gt; handleValidationExceptions(MethodArgumentNotValidException ex) {\n        Map&lt;String, String&gt; errors = new HashMap();\n        ex.getBindingResult().getFieldErrors().forEach(error -&gt; errors.put(error.getField(), error.getDefaultMessage()));\n        return new ResponseEntity(errors, HttpStatus.BAD_REQUEST);\n    }\n}\n<\/pre>\n<p>The code defines:<\/p>\n<ul>\n<li>@RestControllerAdvice: Indicates that the <code>GlobalExceptionHandler<\/code> class is a global exception handler for the entire Spring application, providing centralized error handling for all controllers.<\/li>\n<li>@ExceptionHandler(MethodArgumentNotValidException.class): Specifies that the <code>handleValidationExceptions<\/code> method will handle exceptions of type <code>MethodArgumentNotValidException<\/code>.<\/li>\n<li>handleValidationExceptions(MethodArgumentNotValidException ex): Method that handles validation exceptions thrown during request processing.\n<ul>\n<li>Creates a new <code>HashMap<\/code> called <code>errors<\/code> to store field errors.<\/li>\n<li>Uses <code>ex.getBindingResult().getFieldErrors()<\/code> to iterate through field errors and populate the <code>errors<\/code> map with field names as keys and error messages as values.<\/li>\n<li>Returns a <code>ResponseEntity<\/code> containing the <code>errors<\/code> map and HTTP status <code>HttpStatus.BAD_REQUEST<\/code> (400), indicating that the request was malformed or contained invalid data.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>3.7 Create the Main file<\/h3>\n<p>Create a Spring boot application to initialize the application and hit the controller endpoints.<\/p>\n<pre class=\"brush:java; wrap-lines:false;\">@SpringBootApplication\npublic class MvccrudexampleApplication {\n\n    public static void main(String[] args) {\n        SpringApplication.run(MvccrudexampleApplication.class, args);\n    }\n}\n<\/pre>\n<h3>3.8 Run the application<\/h3>\n<p>Run your Spring Boot application and the application will be started on a port number specified in the application properties file. As soon as the application is started the application endpoints will be initialized and you can use the endpoints to interact with the database to fetch the details.<\/p>\n<pre class=\"brush:plain; wrap-lines:false;\">-- GET http:\/\/localhost:9090\/api\/books: Retrieves all books.\n\n-- GET http:\/\/localhost:9090\/api\/books\/{{id}}: Retrieves a specific book by ID.\n\n-- POST http:\/\/localhost:9090\/api\/books: Creates a new book.\n\n-- POST http:\/\/localhost:9090\/api\/books\nContent-Type: application\/json\n{\n  \"title\": \"\",\n  \"author\": \"\",\n  \"price\": 0\n}\n\n-- PUT http:\/\/localhost:9090\/api\/books\/{{id}}: Updates a specific book by ID.\nContent-Type: application\/json\n{\n  \"id\": 0,\n  \"title\": \"\",\n  \"author\": \"\",\n  \"price\": 0\n}\n\n-- DELETE http:\/\/localhost:9090\/api\/books\/{{id}}: Deletes a specific book by ID.\n<\/pre>\n<p>These endpoints collectively provide CRUD (Create, Read, Update, Delete) operations for managing books in a RESTful manner over HTTP. Each endpoint corresponds to a specific operation on the book entity, facilitating interaction with the server&#8217;s book data through standard HTTP methods and JSON payloads.<\/p>\n<h3>3.9 Unit Testing<\/h3>\n<p>Finally, let&#8217;s write some unit tests for our controller:<\/p>\n<pre class=\"brush:java; wrap-lines:false;\">@WebMvcTest(BookController.class)\npublic class BookControllerTest {\n    @Autowired\n    private MockMvc mockMvc;\n    @MockBean\n    private BookRepository bookRepository;\n    @Autowired\n    private ObjectMapper objectMapper;\n\n    @Test\n    public void shouldReturnAllBooks() throws Exception {\n        Book book = new Book();\n        book.setId(1L);\n        book.setTitle(\"Test Title\");\n        book.setAuthor(\"Test Author\");\n        book.setPrice(19.99);\n\n        given(bookRepository.findAll()).willReturn(List.of(book));\n        mockMvc.perform(get(\"\/api\/books\"))\n                .andExpect(status().isOk())\n                .andExpect(jsonPath(\"$[0].title\").value(book.getTitle()));\n    }\n\n    @Test\n    public void shouldReturnBookById() throws Exception {\n        Book book = new Book();\n        book.setId(1L);\n        book.setTitle(\"Test Title\");\n        book.setAuthor(\"Test Author\");\n        book.setPrice(19.99);\n\n        given(bookRepository.findById(book.getId())).willReturn(Optional.of(book));\n        mockMvc.perform(get(\"\/api\/books\/{id}\", book.getId()))\n                .andExpect(status().isOk())\n                .andExpect(jsonPath(\"$.title\").value(book.getTitle()));\n    }\n\n    @Test\n    public void shouldCreateBook() throws Exception {\n        Book book = new Book();\n        book.setTitle(\"Test Title\");\n        book.setAuthor(\"Test Author\");\n        book.setPrice(19.99);\n\n        given(bookRepository.save(book)).willReturn(book);\n        mockMvc.perform(post(\"\/api\/books\")\n                        .contentType(MediaType.APPLICATION_JSON)\n                        .content(objectMapper.writeValueAsString(book)))\n                .andExpect(status().isOk());\n    }\n\n    @Test\n    public void shouldUpdateBook() throws Exception {\n        Book book = new Book();\n        book.setId(1L);\n        book.setTitle(\"Test Title\");\n        book.setAuthor(\"Test Author\");\n        book.setPrice(19.99);\n\n        Book updatedBook = new Book();\n        updatedBook.setTitle(\"Updated Title\");\n        updatedBook.setAuthor(\"Updated Author\");\n        updatedBook.setPrice(29.99);\n\n        given(bookRepository.findById(book.getId())).willReturn(Optional.of(book));\n        given(bookRepository.save(book)).willReturn(updatedBook);\n        mockMvc.perform(put(\"\/api\/books\/{id}\", book.getId())\n                        .contentType(MediaType.APPLICATION_JSON)\n                        .content(objectMapper.writeValueAsString(updatedBook)))\n                .andExpect(status().isOk())\n                .andExpect(jsonPath(\"$.title\").value(updatedBook.getTitle()));\n    }\n\n    @Test\n    public void shouldDeleteBook() throws Exception {\n        Book book = new Book();\n        book.setId(1L);\n\n        given(bookRepository.findById(book.getId())).willReturn(Optional.of(book));\n        mockMvc.perform(delete(\"\/api\/books\/{id}\", book.getId()))\n                .andExpect(status().isNoContent());\n        verify(bookRepository).delete(book);\n    }\n}\n<\/pre>\n<p>The test case defines:<\/p>\n<ul>\n<li>@WebMvcTest(BookController.class): Specifies that the <code>BookControllerTest<\/code> class is a Spring MVC test for the <code>BookController<\/code> class, focusing on testing the web layer of the application without loading the full Spring context.<\/li>\n<li>@Autowired private MockMvc mockMvc: Autowires (injects) a <code>MockMvc<\/code> instance, which is used to perform HTTP requests against the controller and verify responses.<\/li>\n<li>@MockBean private BookRepository bookRepository: Mocks (creates a mock instance of) the <code>BookRepository<\/code> interface, allowing controlled interactions during testing without accessing a real database.<\/li>\n<li>@Autowired private ObjectMapper objectMapper: Autowires (injects) an <code>ObjectMapper<\/code> instance, used for converting Java objects to JSON and vice versa during request and response handling.<\/li>\n<li>@Test public void shouldReturnAllBooks(): Test method to verify the behavior of retrieving all books from the API.\n<ul>\n<li>Creates a sample <code>Book<\/code> object and sets its attributes.<\/li>\n<li>Mocks the behavior of <code>bookRepository.findAll()<\/code> to return a list containing the sample book.<\/li>\n<li>Performs an HTTP GET request to <code>\/api\/books<\/code> using <code>mockMvc.perform(get(\"\/api\/books\"))<\/code>.<\/li>\n<li>Verifies that the HTTP status returned is <code>isOk()<\/code> (200) and checks that the response JSON contains the expected title of the book using <code>jsonPath(\"$[0].title\").value(book.getTitle())<\/code>.<\/li>\n<\/ul>\n<\/li>\n<li>Other test methods (<code>shouldReturnBookById<\/code>, <code>shouldCreateBook<\/code>, <code>shouldUpdateBook<\/code>, <code>shouldDeleteBook<\/code>): Each test method follows a similar structure:\n<ul>\n<li>Creates necessary <code>Book<\/code> objects and sets their attributes.<\/li>\n<li>Mocks appropriate <code>bookRepository<\/code> methods (<code>findById<\/code>, <code>save<\/code>, <code>delete<\/code>) to simulate database interactions.<\/li>\n<li>Performs HTTP requests (<code>GET<\/code>, <code>POST<\/code>, <code>PUT<\/code>, <code>DELETE<\/code>) to specific endpoints using <code>mockMvc.perform()<\/code>.<\/li>\n<li>Verifies the expected HTTP status and validates response content using assertions like <code>jsonPath<\/code> to ensure the correctness of data returned by the API.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2><a name=\"conclusion\"><\/a>4. Conclusion<\/h2>\n<p>Creating a REST API controller using the <code>RestController<\/code> annotation in a Spring Boot application is a streamlined and efficient way to develop robust and scalable web services. Spring Boot&#8217;s integration with Spring MVC simplifies the configuration and development process, allowing developers to focus on implementing business logic rather than boilerplate code. With a clear understanding of how to set up your project, define endpoints, and handle requests, you can leverage the full power of Spring Boot to build efficient and maintainable RESTful APIs. Whether you are building a small service or a complex system, mastering these skills is essential for modern web development.<\/p>\n<h2><a name=\"download-the-source-code\"><\/a>5. Download the source code<\/h2>\n<p>In this tutorial, we demonstrated how to use <code>RestController<\/code> annotation to perform CRUD operations via the JPA interface.<\/p>\n<div class=\"download\"><strong>Download<\/strong><br \/>You can download the full source code of this example here: <a href=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2024\/06\/Spring-Boot-MVC-REST-Controller-Example-Unit-Tests.zip\"><strong>Spring Boot MVC REST Controller Example &amp; Unit Tests<\/strong><\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In modern web development, REST APIs are essential for enabling communication between different systems and platforms. The Spring Framework, particularly Spring Boot, provides robust support for building RESTful web services, simplifying the development process through various annotations and tools. One such powerful annotation is @RestController, which is used in Spring MVC to create RESTful API &hellip;<\/p>\n","protected":false},"author":26931,"featured_media":112,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[513,657,1708,854,150,244],"class_list":["post-124031","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-enterprise-java","tag-crud","tag-postgresql","tag-rest-api","tag-spring-boot","tag-spring-mvc","tag-sql"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Spring Boot MVC REST Controller Example &amp; Unit Tests - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"Spring boot Rest controller example: Learn how to create a REST API controller in Spring Boot using the @RestController annotation.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.javacodegeeks.com\/spring-boot-mvc-rest-controller-example-unit-tests.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Spring Boot MVC REST Controller Example &amp; Unit Tests - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"Spring boot Rest controller example: Learn how to create a REST API controller in Spring Boot using the @RestController annotation.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/spring-boot-mvc-rest-controller-example-unit-tests.html\" \/>\n<meta property=\"og:site_name\" content=\"Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2024-06-27T09:12:00+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Yatin Batra\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Yatin Batra\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/spring-boot-mvc-rest-controller-example-unit-tests.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/spring-boot-mvc-rest-controller-example-unit-tests.html\"},\"author\":{\"name\":\"Yatin Batra\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/cda31a4c1965373fed40c8907dc09b8d\"},\"headline\":\"Spring Boot MVC REST Controller Example &amp; Unit Tests\",\"datePublished\":\"2024-06-27T09:12:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/spring-boot-mvc-rest-controller-example-unit-tests.html\"},\"wordCount\":1542,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/spring-boot-mvc-rest-controller-example-unit-tests.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/enterprise-java-logo.jpg\",\"keywords\":[\"CRUD\",\"PostgreSQL\",\"REST API\",\"Spring Boot\",\"Spring MVC\",\"SQL\"],\"articleSection\":[\"Enterprise Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/spring-boot-mvc-rest-controller-example-unit-tests.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/spring-boot-mvc-rest-controller-example-unit-tests.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/spring-boot-mvc-rest-controller-example-unit-tests.html\",\"name\":\"Spring Boot MVC REST Controller Example &amp; Unit Tests - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/spring-boot-mvc-rest-controller-example-unit-tests.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/spring-boot-mvc-rest-controller-example-unit-tests.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/enterprise-java-logo.jpg\",\"datePublished\":\"2024-06-27T09:12:00+00:00\",\"description\":\"Spring boot Rest controller example: Learn how to create a REST API controller in Spring Boot using the @RestController annotation.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/spring-boot-mvc-rest-controller-example-unit-tests.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/spring-boot-mvc-rest-controller-example-unit-tests.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/spring-boot-mvc-rest-controller-example-unit-tests.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/enterprise-java-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/enterprise-java-logo.jpg\",\"width\":150,\"height\":150,\"caption\":\"java-interview-questions-answers\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/spring-boot-mvc-rest-controller-example-unit-tests.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Enterprise Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\\\/enterprise-java\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Spring Boot MVC REST Controller Example &amp; Unit Tests\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Developers Resource Center\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.javacodegeeks.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/javacodegeeks\",\"https:\\\/\\\/x.com\\\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/cda31a4c1965373fed40c8907dc09b8d\",\"name\":\"Yatin Batra\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/12\\\/Yatin.batra_.jpg\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/12\\\/Yatin.batra_.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/12\\\/Yatin.batra_.jpg\",\"caption\":\"Yatin Batra\"},\"description\":\"An experience full-stack engineer well versed with Core Java, Spring\\\/Springboot, MVC, Security, AOP, Frontend (Angular &amp; React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).\",\"sameAs\":[\"https:\\\/\\\/www.javacodegeeks.com\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/yatin-batra\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Spring Boot MVC REST Controller Example &amp; Unit Tests - Java Code Geeks","description":"Spring boot Rest controller example: Learn how to create a REST API controller in Spring Boot using the @RestController annotation.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.javacodegeeks.com\/spring-boot-mvc-rest-controller-example-unit-tests.html","og_locale":"en_US","og_type":"article","og_title":"Spring Boot MVC REST Controller Example &amp; Unit Tests - Java Code Geeks","og_description":"Spring boot Rest controller example: Learn how to create a REST API controller in Spring Boot using the @RestController annotation.","og_url":"https:\/\/www.javacodegeeks.com\/spring-boot-mvc-rest-controller-example-unit-tests.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2024-06-27T09:12:00+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","type":"image\/jpeg"}],"author":"Yatin Batra","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Yatin Batra","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/spring-boot-mvc-rest-controller-example-unit-tests.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/spring-boot-mvc-rest-controller-example-unit-tests.html"},"author":{"name":"Yatin Batra","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/cda31a4c1965373fed40c8907dc09b8d"},"headline":"Spring Boot MVC REST Controller Example &amp; Unit Tests","datePublished":"2024-06-27T09:12:00+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/spring-boot-mvc-rest-controller-example-unit-tests.html"},"wordCount":1542,"commentCount":0,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/spring-boot-mvc-rest-controller-example-unit-tests.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","keywords":["CRUD","PostgreSQL","REST API","Spring Boot","Spring MVC","SQL"],"articleSection":["Enterprise Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/spring-boot-mvc-rest-controller-example-unit-tests.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/spring-boot-mvc-rest-controller-example-unit-tests.html","url":"https:\/\/www.javacodegeeks.com\/spring-boot-mvc-rest-controller-example-unit-tests.html","name":"Spring Boot MVC REST Controller Example &amp; Unit Tests - Java Code Geeks","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/spring-boot-mvc-rest-controller-example-unit-tests.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/spring-boot-mvc-rest-controller-example-unit-tests.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","datePublished":"2024-06-27T09:12:00+00:00","description":"Spring boot Rest controller example: Learn how to create a REST API controller in Spring Boot using the @RestController annotation.","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/spring-boot-mvc-rest-controller-example-unit-tests.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/spring-boot-mvc-rest-controller-example-unit-tests.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/spring-boot-mvc-rest-controller-example-unit-tests.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","width":150,"height":150,"caption":"java-interview-questions-answers"},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/spring-boot-mvc-rest-controller-example-unit-tests.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java","item":"https:\/\/www.javacodegeeks.com\/category\/java"},{"@type":"ListItem","position":3,"name":"Enterprise Java","item":"https:\/\/www.javacodegeeks.com\/category\/java\/enterprise-java"},{"@type":"ListItem","position":4,"name":"Spring Boot MVC REST Controller Example &amp; Unit Tests"}]},{"@type":"WebSite","@id":"https:\/\/www.javacodegeeks.com\/#website","url":"https:\/\/www.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Developers Resource Center","publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/cda31a4c1965373fed40c8907dc09b8d","name":"Yatin Batra","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/12\/Yatin.batra_.jpg","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/12\/Yatin.batra_.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/12\/Yatin.batra_.jpg","caption":"Yatin Batra"},"description":"An experience full-stack engineer well versed with Core Java, Spring\/Springboot, MVC, Security, AOP, Frontend (Angular &amp; React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).","sameAs":["https:\/\/www.javacodegeeks.com"],"url":"https:\/\/www.javacodegeeks.com\/author\/yatin-batra"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/124031","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/users\/26931"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=124031"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/124031\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/112"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=124031"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=124031"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=124031"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}