package hello.controllers;

import java.io.IOException;

import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
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.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import models.Greeting;

@RestController
@RequestMapping(value = "/api", produces = { MediaType.APPLICATION_JSON_VALUE })
public class ApiController {

	@RequestMapping(value = "/greeting", method = RequestMethod.GET)
	public @ResponseBody ResponseEntity<Greeting> get() {
		Greeting greeting = new Greeting("World");
		return new ResponseEntity<Greeting>(greeting, HttpStatus.OK);
	}

	@RequestMapping(value = "/greeting", method = RequestMethod.POST)
	public @ResponseBody ResponseEntity<Greeting> post(@RequestBody String body) {
		Greeting greeting = new Greeting(getNameFromJson(body));
		return new ResponseEntity<Greeting>(greeting, HttpStatus.OK);
	}
	
	private String getNameFromJson(String json) {
		ObjectMapper objectMapper = new ObjectMapper();

		JsonNode rootNode;
		String name;
		
		try {
			rootNode = objectMapper.readTree(json);
			name = rootNode.path("name").textValue();
		} catch (IOException e) {
			name = "World";
		}
		
		return name;
	}

}

@@ -748,16 +748,70 @@
 ller %7B%0A%0A
+%09private static final String DEFAULT_NAME = %22World%22;%0A%0A
 %09@Reques
@@ -958,23 +958,28 @@
 reeting(
-%22World%22
+DEFAULT_NAME
 );%0A%09%09ret
@@ -1243,16 +1243,30 @@
 son(body
+, DEFAULT_NAME
 ));%0A%09%09re
@@ -1328,17 +1328,16 @@
 OK);%0A%09%7D%0A
-%09
 %0A%09privat
@@ -1372,16 +1372,37 @@
 ing json
+, String default_name
 ) %7B%0A%09%09Ob
@@ -1482,20 +1482,25 @@
 ing name
+ = null
 ;%0A
-%09%09
 %0A%09%09try %7B
@@ -1623,30 +1623,118 @@
 %0A%09%09%09
-name = %22World%22
+// We can ignore this error, as we set a default below%0A%09%09%7D%0A%0A%09%09if (name == null) %7B%0A%09%09%09name = default_name
 ;%0A%09%09%7D%0A
-%09%09
 %0A%09%09r

Set a default name in the API controller.