How to Create a REST API With Spring Boot

In previous post we have learn how to change port of server . If you new one then follow spring boot tutorial example here .  In this tutorial we will learn restful web services  with spring boot . rest services is very important topic for java developers . If you are new then first read how to create spring boot application  . Now are we going to restful web services example in java with spring boot.

Firstly we will create controller , it is endpoint of our rest api’s.

HelloContoller.java

package com.jp.helloWorld.controller;

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

import com.jp.helloWorld.model.User;

@RestController
public class HelloController {

	
	@GetMapping("/user")
	public User getApplicationName(){
		User user = new User( 10L, "shivam");
		return user;
	   }
	}

Now we will create model class user

User.java

package com.jp.helloWorld.model;

public class User {
	private Long userId;
	private String name;
	
	public User(){
		
	}
	
	public User(Long userId, String name) {
		super();
		this.userId = userId;
		this.name = name;
	}

   public Long getUserId() {
		return userId;
	}


	public String getName() {
		return name;
	}

	

}

Now run your spring boot application and hit url http://localhost:8080/user . then you will  got

{"userId":10,"name":"shivam"}

Response

You can this spring boot example below.

   Download Rest Application

Leave a Reply

Your email address will not be published. Required fields are marked *

45 − 43 =