Monday, January 24, 2022

How to start with Rest Controller from basic







 

Let's Begin Rest Controller Using Spring Boot

RestController is a Spring annotation that is used to build REST API 


Step1: open dilsecodie website. 


Step2: GOTO Spring boot section, then rest controller.


Step3: You can check @PutMapping is put method, we can use any method post or get, it's just an example, you can see request parameter in a method, we can take these values in model class also.


Step4: Now make your service, then use its business logic, then How we can send any Response for this you can use the way dilsecodie is using.

Spring Boot complements Spring REST support by providing default 

dependencies/converters out of the box. Writing RESTful services 

in Spring Boot is no different than Spring MVC. 

If you are a REST Client,

Spring Boot provides RestTemplateBuilder.

Its simple, for example you can build REST services which accepts get request. Let start from the web.xml


<!DOCTYPE web-app PUBLIC 

 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 

 "http://java.sun.com/dtd/web-app_2_3.dtd" > 

 

<web-app> 

  <display-name>Spring World</display-name> 

   

  <servlet> 

    <servlet-name>dispatcher</servlet-name> 

    <servlet-class> 

        org.springframework.web.servlet.DispatcherServlet 

    </servlet-class> 

    <init-param> 

        <param-name>contextConfigLocation</param-name> 

        <param-value>/WEB-INF/spring-servlet.xml</param-value> 

    </init-param> 

    <load-on-startup>1</load-on-startup> 

 </servlet> 

  

 <servlet-mapping> 

    <servlet-name>dispatcher</servlet-name> 

    <url-pattern>/</url-pattern> 

 </servlet-mapping> 

</web-app> 

Here is the spring-servlet.xml


<beans xmlns="http://www.springframework.org/schema/beans" 

    xmlns:context="Index of /schema/context" 

    xmlns:mvc="Index of /schema/mvc" 

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

    xsi:schemaLocation="Index of /schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 

    Index of /schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 

    Index of /schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> 

  

    <context:component-scan base-package="org.gajendra.springworld.controllers" /> 

  

    <mvc:annotation-driven /> 

      

    <bean 

        class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 

        <property name="prefix"> 

            <value>/WEB-INF/views/</value> 

        </property> 

        <property name="suffix"> 

            <value>.jsp</value> 

        </property> 

    </bean> 

  

</beans> 

Following are the depedencies for this.


Note: I am using JDK 1.7.x


  <dependencies> 

   

   <dependency> 

            <groupId>org.springframework</groupId> 

            <artifactId>spring-webmvc</artifactId> 

            <version>4.0.6</version> 

        </dependency> 

  

         

        <dependency> 

            <groupId>javax.servlet</groupId> 

            <artifactId>javax.servlet-api</artifactId> 

            <version>3.1.0</version> 

        </dependency> 

        <dependency> 

            <groupId>javax.servlet.jsp</groupId> 

            <artifactId>javax.servlet.jsp-api</artifactId> 

            <version>2.3.1</version> 

        </dependency> 

        <dependency> 

            <groupId>javax.servlet</groupId> 

            <artifactId>jstl</artifactId> 

            <version>1.2</version> 

        </dependency> 

 

<dependency> 

<groupId>com.fasterxml.jackson.core</groupId> 

<artifactId>jackson-databind</artifactId> 

<version>2.5.3</version> 

</dependency> 

         

         

    <dependency> 

      <groupId>junit</groupId> 

      <artifactId>junit</artifactId> 

      <version>3.8.1</version> 

      <scope>test</scope> 

    </dependency> 

     

  </dependencies> 

package org.dilsecodie.controllers; 

 

import javax.servlet.http.HttpServletRequest; 

 

import org.gajendra.springworld.model.Message; 

import org.springframework.web.bind.annotation.PathVariable; 

import org.springframework.web.bind.annotation.RequestMapping; 

import org.springframework.web.bind.annotation.RestController; 

 

@RestController 

public class HelloWorldRestController { 

 

  @RequestMapping("/hello/{employee}") 

    public Message message(@PathVariable String player, HttpServletRequest request) { 

  String text= "Welcome!"; 

        Message msg = new Message(text, " Hello " + employee); 

        return msg; 

    } 

 

Note*: IF still facing how to use this code then ask in the doubt section, our expert community will help you grow.

  

No comments:

Post a Comment