Last active
November 17, 2015 09:09
-
-
Save AlexCzar/99056a0f6928735717ed to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package controllers | |
import com.fasterxml.jackson.databind.ObjectMapper | |
import com.fasterxml.jackson.databind.SerializationFeature | |
import com.fasterxml.jackson.datatype.jsr310.JSR310Module | |
import org.springframework.boot.SpringApplication | |
import org.springframework.boot.autoconfigure.SpringBootApplication | |
import org.springframework.context.annotation.Bean | |
import org.springframework.context.annotation.Configuration | |
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter | |
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter | |
@SpringBootApplication | |
open class Application { | |
companion object { | |
@JvmStatic public fun main(args: Array<String>) { | |
SpringApplication.run(Application::class.java, *args) | |
} | |
} | |
} | |
@Configuration | |
open class WebConfig() : WebMvcConfigurerAdapter() { | |
@Bean | |
open fun converter(): MappingJackson2HttpMessageConverter { | |
val mapper = ObjectMapper(); | |
mapper.registerModule(JSR310Module()); | |
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); | |
return MappingJackson2HttpMessageConverter(mapper); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package controllers | |
import com.fasterxml.jackson.annotation.JsonCreator | |
import com.fasterxml.jackson.annotation.JsonProperty | |
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.RestController | |
@RestController | |
@RequestMapping("/events") | |
open class EventsController { | |
val events: MutableList<Event> = arrayListOf( | |
Event( | |
host = Host("1", "Czar"), | |
activity = "playing poker", | |
invited = listOf()), | |
Event( | |
host = Host("1", "Czar"), | |
activity = "playing Dominion", | |
invited = listOf())) | |
/** | |
give it POST `{"host":{"id":"1","name":"Czar"},"activity":"playing RPG","invited":[]}` and you'll get: | |
`.c.j.MappingJackson2HttpMessageConverter : Failed to evaluate deserialization for type [simple type, class controllers.Event]: com.fasterxml.jackson.databind.JsonMappingException: Argument #0 of constructor [constructor for controllers.Guest, annotations: {interface com.fasterxml.jackson.annotation.JsonCreator=@com.fasterxml.jackson.annotation.JsonCreator()}] has no property name annotation; must have name when multiple-parameter constructor annotated as Creator` | |
*/ | |
@RequestMapping(method = arrayOf(RequestMethod.POST), value = "") | |
fun post(@RequestBody Event: Event): List<Event> { | |
events.add(Event) | |
return events.toList() | |
} | |
} | |
data class Host @JsonCreator constructor( | |
@JsonProperty("id") val id: String, @JsonProperty("name") val name: String) | |
data class Event @JsonCreator constructor( | |
@JsonProperty("host") val host: Host, | |
@JsonProperty("activity") val activity: String, | |
@JsonProperty("invited") val invited: List<Guest>) | |
data class Guest @JsonCreator constructor( | |
@JsonProperty("id") val id: String, @JsonProperty("name") val name: String, | |
@JsonProperty("rsvp") var rsvp: RSVP = RSVP.interested) | |
enum class RSVP(val nameKey: String) { | |
going("rsvp.going"), maybe("rsvp.maybe"), interested("rsvp.interested") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment