Skip to content

Instantly share code, notes, and snippets.

@junquera
Created March 11, 2020 06:36
Show Gist options
  • Save junquera/86803ce48361906d69f7a7c77ebf5cd4 to your computer and use it in GitHub Desktop.
Save junquera/86803ce48361906d69f7a7c77ebf5cd4 to your computer and use it in GitHub Desktop.
Spring basic serialization example
import io.junquera.ca.back.entities.User;
import org.json.JSONObject;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class LogController {
@PostMapping("/user")
public void user(@RequestBody User u) throws JsonProcessingException {
System.out.println(u.getUsername()));
}
@PutMapping("/user")
public void rawUser(@RequestBody String jsonRaw) throws JsonProcessingException {
ObjectMapper mapper=new ObjectMapper();
User u = mapper.readValue(jsonRaw, User.class);
JSONObject json = new JSONObject(jsonRaw);
System.out.println(json.keySet());
System.out.println("Correct: " + (json.get("username") == u.getUsername()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment