Improving spring example

This commit is contained in:
Guillaume Polaert 2017-08-01 12:34:00 +02:00
parent 65bfaef8b5
commit 97e7228ce6
5 changed files with 42 additions and 24 deletions

View File

@ -20,7 +20,7 @@ public class User {
return id;
}
public void setId(Integer id) {
public void setId(final Integer id) {
this.id = id;
}
@ -28,7 +28,7 @@ public class User {
return name;
}
public void setName(String name) {
public void setName(final String name) {
this.name = name;
}
@ -36,7 +36,7 @@ public class User {
return email;
}
public void setEmail(String email) {
public void setEmail(final String email) {
this.email = email;
}
}

View File

@ -5,4 +5,5 @@ import org.springframework.data.repository.CrudRepository;
// This will be AUTO IMPLEMENTED by Spring into a Bean called userRepository
// CRUD refers Create, Read, Update, Delete
public interface UserRepository extends CrudRepository<User, Long> {}
public interface UserRepository extends CrudRepository<User, Integer> {
}

View File

@ -10,19 +10,20 @@ import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller // This means that this class is a Controller
@RequestMapping(path = "/demo") // This means URL's start with /demo (after Application path)
public class MySQLResource {
@RequestMapping(path = "/user") // This means URL's start with /demo (after Application path)
public class DBResource {
@Autowired // This means to get the bean called userRepository
// Which is auto-generated by Spring, we will use it to handle the data
private UserRepository userRepository;
@GetMapping(path = "/add") // Map ONLY GET Requests
public @ResponseBody String addNewUser(@RequestParam String name, @RequestParam String email) {
public @ResponseBody
String addNewUser(@RequestParam final String name, @RequestParam final String email) {
// @ResponseBody means the returned String is the response, not a view name
// @RequestParam means it is a parameter from the GET or POST request
User n = new User();
final User n = new User();
n.setName(name);
n.setEmail(email);
userRepository.save(n);
@ -34,4 +35,11 @@ public class MySQLResource {
// This returns a JSON or XML with the users
return userRepository.findAll();
}
@GetMapping(path = "/get")
public @ResponseBody
User getUser(@RequestParam final int id) {
// This returns a JSON or XML with the users
return userRepository.findOne(id);
}
}

View File

@ -1,16 +0,0 @@
package com.datadoghq.examples.resources;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/")
public class HelloResource {
@RequestMapping(method = RequestMethod.GET)
public String test() {
return "Hello world!";
}
}

View File

@ -0,0 +1,25 @@
package com.datadoghq.examples.resources;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/")
public class HomeResource {
@RequestMapping(method = RequestMethod.GET)
public String test() {
final StringBuilder template = new StringBuilder();
template.append("Demo links");
template.append("<ul>");
template.append("<li><a href=\"/user/add?name=name&email=f@example.com\">Add a user</a></li>");
template.append("<li><a href=\"/user/all\">List all users</a></li>");
template.append("<li><a href=\"/user/get?id=1\">Get user with id=1</a></li>");
template.append("</ul>");
return template.toString();
}
}