|
| 1 | +package com.foo.rest.examples.spring.openapi.v3.httporacle.nonidempotentput.xml |
| 2 | + |
| 3 | +import org.springframework.boot.SpringApplication |
| 4 | +import org.springframework.boot.autoconfigure.SpringBootApplication |
| 5 | +import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration |
| 6 | +import org.springframework.http.MediaType |
| 7 | +import org.springframework.http.ResponseEntity |
| 8 | +import org.springframework.web.bind.annotation.GetMapping |
| 9 | +import org.springframework.web.bind.annotation.PathVariable |
| 10 | +import org.springframework.web.bind.annotation.PostMapping |
| 11 | +import org.springframework.web.bind.annotation.PutMapping |
| 12 | +import org.springframework.web.bind.annotation.RequestBody |
| 13 | +import org.springframework.web.bind.annotation.RequestMapping |
| 14 | +import org.springframework.web.bind.annotation.RestController |
| 15 | +import javax.xml.bind.annotation.XmlAccessType |
| 16 | +import javax.xml.bind.annotation.XmlAccessorType |
| 17 | +import javax.xml.bind.annotation.XmlRootElement |
| 18 | + |
| 19 | + |
| 20 | +@SpringBootApplication(exclude = [SecurityAutoConfiguration::class]) |
| 21 | +@RequestMapping(path = ["/api/accounts"]) |
| 22 | +@RestController |
| 23 | +open class HttpNonIdempotentPutXMLApplication { |
| 24 | + |
| 25 | + companion object { |
| 26 | + @JvmStatic |
| 27 | + fun main(args: Array<String>) { |
| 28 | + SpringApplication.run(HttpNonIdempotentPutXMLApplication::class.java, *args) |
| 29 | + } |
| 30 | + |
| 31 | + private val data = mutableMapOf<Int, AccountData>() |
| 32 | + |
| 33 | + fun reset(){ |
| 34 | + data.clear() |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + @XmlRootElement(name = "accountData") |
| 39 | + @XmlAccessorType(XmlAccessType.FIELD) |
| 40 | + open class AccountData( |
| 41 | + var balance: Int = 0 |
| 42 | + ) |
| 43 | + |
| 44 | + @XmlRootElement(name = "depositRequest") |
| 45 | + @XmlAccessorType(XmlAccessType.FIELD) |
| 46 | + open class DepositRequest( |
| 47 | + var amount: Int = 0 |
| 48 | + ) |
| 49 | + |
| 50 | + |
| 51 | + @PostMapping( |
| 52 | + consumes = [MediaType.APPLICATION_XML_VALUE], |
| 53 | + produces = [MediaType.APPLICATION_XML_VALUE] |
| 54 | + ) |
| 55 | + open fun create(@RequestBody body: AccountData): ResponseEntity<AccountData> { |
| 56 | + val id = data.size + 1 |
| 57 | + data[id] = AccountData(balance = body.balance) |
| 58 | + return ResponseEntity.status(201).body(data[id]) |
| 59 | + } |
| 60 | + |
| 61 | + @GetMapping( |
| 62 | + path = ["/{id}"], |
| 63 | + produces = [MediaType.APPLICATION_XML_VALUE] |
| 64 | + ) |
| 65 | + open fun get(@PathVariable("id") id: Int): ResponseEntity<AccountData> { |
| 66 | + val resource = data[id] |
| 67 | + ?: return ResponseEntity.status(404).build() |
| 68 | + return ResponseEntity.status(200).body(resource) |
| 69 | + } |
| 70 | + |
| 71 | + @PutMapping( |
| 72 | + path = ["/{id}/deposit"], |
| 73 | + consumes = [MediaType.APPLICATION_XML_VALUE], |
| 74 | + produces = [MediaType.APPLICATION_XML_VALUE] |
| 75 | + ) |
| 76 | + open fun deposit( |
| 77 | + @PathVariable("id") id: Int, |
| 78 | + @RequestBody body: DepositRequest |
| 79 | + ): ResponseEntity<AccountData> { |
| 80 | + |
| 81 | + val resource = data[id] |
| 82 | + ?: return ResponseEntity.status(404).build() |
| 83 | + |
| 84 | + // wrong: PUT must be idempotent, but each call accumulates the deposit |
| 85 | + resource.balance += body.amount |
| 86 | + |
| 87 | + return ResponseEntity.status(200).body(resource) |
| 88 | + } |
| 89 | +} |
0 commit comments