+-+-+ +-+ +-+-+-+-+
|G|O| |4| |H|I|V|E|
+-+-+ +-+ +-+-+-+-+

 --- A GOPHER-LIKE INTERFACE FOR HIVE BLOCKCHAIN ---

How to develop Rest Web Service using @RestController - Spring Framework

BY: @ravik2492 | CREATED: Jan. 15, 2018, 10:42 a.m. | VOTES: 10 | PAYOUT: $0.06 | [ VOTE ]

[IMAGE: https://www.javatpoint.com/images/spimages/spring1.png]

SourceHere

What Will I Learn?

In this tutorial, we will learn about how we can develop Rest web API or Services using @RestController annotation in Spring framework and how you can call the web services using ARC Client

Requirements

Difficulty

Tutorial Contents

Now we configure the web application with sping framework to creating @restController

  1. configuring web.xml file
    First of all paste all jar files into lib folder
    Here we configure web.xml file to run application application



        spring
        org.springframework.web.servlet.DispatcherServlet

            contextConfigLocation
            /WEB-INF/config/spring-servlet.xml

        1


        spring
        /





[IMAGE: https://res.cloudinary.com/hpiynhbhq/image/upload/v1516012856/sl4tmx5pf01oo2jkqofb.png]

  1. configuring spring-servlet.xml
    Basic configuration of databse, view resolver and sessionfactory









    ```
            com.blog.model.Users




            ${hibernate.dialect}
            ${hibernate.show_sql}
            ${hibernate.hbm2ddl.auto}


[IMAGE: https://res.cloudinary.com/hpiynhbhq/image/upload/v1516012832/wq4boushyywgq6yoplrb.png]

package com.blog.restwebservice;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.blog.bean.QuestionsBean;
import beans.ConvertDateFormat;
import beans.ListToJsonObject;
import beans.Sudent;
import com.blog.model.Questions;
import com.blog.service.QuestionsService;

@RestController
@RequestMapping("/yayayaa")
public class RestWebServiceController {
    @Autowired(required=false)
    private QuestionsService questionsService;  
    ListToJsonObject listtojson = new ListToJsonObject();
    ConvertDateFormat fmt = new ConvertDateFormat();    
    @RequestMapping(method = RequestMethod.GET)
    public @ResponseBody String listQuestionsss() throws JsonGenerationException, JsonMappingException, IOException, ParseException {                       
        List list = listofStudents();   
        String json = new ObjectMapper().writeValueAsString(list);
        return json;
    }       
    List list = new ArrayList();
    public List listofStudents()
    {
        list.add(new Sudent(1,20,"Rahul","Whatever","rahul@gmail.com"));
        list.add(new Sudent(2,21,"Ramesh","Whatever","ramesh@gmail.com"));
        list.add(new Sudent(3,22,"Shyam","Whatever","shyam@gmail.com"));
        list.add(new Sudent(4,23,"Ram","Whatever","ram@gmail.com"));
        list.add(new Sudent(5,24,"Krishna","Whatever","krishna@gmail.com"));
        list.add(new Sudent(6,25,"Jacop","Whatever","jacop@gmail.com"));        
        return list;
    }
}

[IMAGE: https://res.cloudinary.com/hpiynhbhq/image/upload/v1516012892/bhzpvotg0rsp48hef9sv.png]

Result

It's Done !
Congratulations ! we successfully developed Rest Web Service.

Thank You !

Share from the core of my heart

Posted on Utopian.io - Rewarding Open Source Contributors

TAGS: [ #utopian-io ] [ #utopian-io ] [ #spring ] [ #restwebservices ] [ #java ]

Replies

@neelamgahlyan | Jan. 15, 2018, 10:44 a.m. | Votes: 0 | [ VOTE ]

Great work. Make a complete tutorial

@ravik2492 | Jan. 15, 2018, 10:46 a.m. | Votes: 0 | [ VOTE ]

Thank you ! 2 hours to complete the work and half hour to write tutorial

@manishmike10 | Jan. 16, 2018, 2:12 p.m. | Votes: 0 | [ VOTE ]

Your contribution cannot be approved because it does not follow the Utopian Rules.
* Wrong repository
Rest controller is not open_ Source.
You can contact us on Discord.
[utopian-moderator]

[ BACK TO TRENDING ] [ BACK TO MENU ]
CMD>