GSON – fromJSON

Obiekty:

public class Dictionary {

	private Integer id;
	private String value;

	public Dictionary() {
	}

	public Dictionary(Integer id, String value) {
		this.id = id;
		this.value = value;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}
	
	public String getLabel() {
		return toString();
	}

}


public class DictionaryCategory extends Dictionary {
	
	public DictionaryCategory() {
	}
	
	public DictionaryCategory(Integer id, String value) {
		super(id, value);
	}

	public String getLabel() {
		return toString();
	}

}
public class Author {

	private Integer id;
	private String firstName;
	private String lastName;
	
	public Author() {
	}
	
	public Author(Integer id, String firstName, String lastName) {
		this.id = id;
		this.firstName = firstName;
		this.lastName = lastName;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}
	
	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	
	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	
	public String getLabel() {
		return toString();
	}

}
public class Book {

	private Integer id;
	private String title;
	private String isbn;
	private Set<Author> authors;
	private DictionaryCategory category;
	
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}
	
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}
	
	public String getIsbn() {
		return isbn;
	}

	public void setIsbn(String isbn) {
		this.isbn = isbn;
	}
	
	public Set<Author> getAuthors() {
		return authors;
	}

	public void setAuthors(Set<Author> authors) {
		this.authors = authors;
	}
	
	public DictionaryCategory getCategory() {
		return category;
	}

	public void setCategory(DictionaryCategory category) {
		this.category = category;
	}
	
	public String getLabel() {
		return toString();
	}

}

JSON:

{
	"id":1,
	"title":"Title",
	"isbn":"ISBN",
	"authors":[
		{
			"id":1,
			"firstName":"FirstName",
			"lastName":"LastName"
		}
	],
	"category":{
		"id":1,
		"value":"Category"
	}
}

Magia:

Book book = new Gson().fromJson(json, Book.class);

Wynik:

Book@65534fec[
	id=1 
	title=Title 
	isbn=ISBN 
	authors=[1,FirstName,LastName] 
	category=1,Category 
]

GSON – toJson

Obiekty:

public class Dictionary {

	private Integer id;
	private String value;

	public Dictionary() {
	}

	public Dictionary(Integer id, String value) {
		this.id = id;
		this.value = value;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}
	
	public String getLabel() {
		return toString();
	}

}


public class DictionaryCategory extends Dictionary {
	
	public DictionaryCategory() {
	}
	
	public DictionaryCategory(Integer id, String value) {
		super(id, value);
	}

	public String getLabel() {
		return toString();
	}

}
public class Author {

	private Integer id;
	private String firstName;
	private String lastName;
	
	public Author() {
	}
	
	public Author(Integer id, String firstName, String lastName) {
		this.id = id;
		this.firstName = firstName;
		this.lastName = lastName;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}
	
	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	
	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	
	public String getLabel() {
		return toString();
	}

}
public class Book {

	private Integer id;
	private String title;
	private String isbn;
	private Set<Author> authors;
	private DictionaryCategory category;
	
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}
	
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}
	
	public String getIsbn() {
		return isbn;
	}

	public void setIsbn(String isbn) {
		this.isbn = isbn;
	}
	
	public Set<Author> getAuthors() {
		return authors;
	}

	public void setAuthors(Set<Author> authors) {
		this.authors = authors;
	}
	
	public DictionaryCategory getCategory() {
		return category;
	}

	public void setCategory(DictionaryCategory category) {
		this.category = category;
	}
	
	public String getLabel() {
		return toString();
	}

}

Magia:

Book book = new Book();
book.setId(1);
book.setTitle("Title");
book.setIsbn("ISBN");
book.setCategory(new DictionaryCategory(1, "Category"));
book.setAuthors(Collections.singleton(new Author(1, "FirstName", "LastName")));
		
new Gson().toJson(getTestBook());

Wynik:

{
	"id":1,
	"title":"Title",
	"isbn":"ISBN",
	"authors":[
		{
			"id":1,
			"firstName":"FirstName",
			"lastName":"LastName"
		}
	],
	"category":{
		"id":1,
		"value":"Category"
	}
}

Lista wyjątków przypisana do kodów błędów HTTP

  • MissingServletRequestParameterException 400 (Bad Request)
  • ServletRequestBindingException 400 (Bad Request)
  • TypeMismatchException 400 (Bad Request)
  • HttpMessageNotReadableException 400 (Bad Request)
  • MethodArgumentNotValidException 400 (Bad Request)
  • MissingServletRequestPartException 400 (Bad Request)
  • NoSuchRequestHandlingMethodException 404 (Not Found)
  • HttpRequestMethodNotSupportedException 405 (Method Not Allowed)
  • HttpMediaTypeNotSupportedException 415 (Unsupported Media Type)
  • ConversionNotSupportedException 500 (Internal Server Error)
  • HttpMessageNotWritableException 500 (Internal Server Error)

http://static.springsource.org/spring/docs/3.2.0.RELEASE/javadoc-api/org/springframework/web/servlet/mvc/support/DefaultHandlerExceptionResolver.html