Reputation: 4149
I searched for several approaches to create restful web services and finally narrowed my current requirement to learn how to create restful web services using spring.
I am new to both spring and restful services design. Tried googling for several hours to get one good post which can help me learn this technology.
can some one point me to a good tutorial which explains how to create restful web services using spring?
All tutorials I came across use POM.XML. Do I really need to use maven to create a restful web service? I am confused. Please help.
Upvotes: 2
Views: 11598
Reputation: 1
User List call using spring rest web service
In this example we have
1)Rest Controller class
2)User Dto Class
@RestController
@RequestMapping("/user")
public class UserMgmt
{
@GetMapping(value = "/getUser")
public ResponseEntity<List<UserDto>> getUser() {
List<UserDto> userDto=new ArrayList<UserDto>();
UserDto obj1=new UserDto();
obj1.setUsername("sibin");
obj1.setEmail("[email protected]");
obj1.setPhone("9895954561");
obj1.setAddress("some thing");
userDto.add(obj1);
UserDto obj2=new UserDto();
obj2.setUsername("user");
obj2.setEmail("[email protected]");
obj2.setPhone("9895954561");
obj2.setAddress("some thing");
userDto.add(obj2);
return new ResponseEntity<List<UserDto>>(userDto,HttpStatus.OK);
}
}
UserDto
public class UserDtos {
private String username;
private String email;
private String phone;
private String address;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return Address;
}
public void setAddress(String address) {
Address = address;
}
}
Output When we call web service we get values in JSON format like following blocks:
[
{
username: "sibin",
email: "[email protected]",
phone: "9895954561",
address: "some thing",
},
{
username: "user",
email: "[email protected]",
phone: "9895954561",
address: "1512381877232",
}]
Response is sent as JSON string using below dependancy
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.3</version>
</dependency>
Upvotes: 0
Reputation: 539
I know i am late by 6 months. By now you would have become a master at Web Services with Spring. But i recently was trying to learn this and this example helped me a lot. http://software.sawano.se/2012/03/combining-json-and-xml-in-restful-web.html
This example is very simple and does not have lots of Dependencies. Just need to install Maven, and Eclipse and a Server. hopefully it will be of use to someone else in the future. Thanks.
Upvotes: 3
Reputation: 22382
The best spring rest tutorial that I came across are these two:
Also you can download the code from git by using this command (you need to install git it first of course)
git clone https://code.google.com/p/bti360/
Here is another good tutorial and you can download the code similar to previous tutorial and run it in your STS or eclipse.
I have tested these two on my localhost and they both work well. For the second link you need to install Gradle to make your build process easy and painless.
Upvotes: 2
Reputation: 1702
I think it's better to start from scratch. Just create a project in your IDE and add the jars and configurations step by step. It's not easy to read hundreds of lines of XML written by others from the start, at least for me.
Here is a very simple tutorial, hope it helps.
Upvotes: 1