|
| 1 | +package com.foo.rest.examples.spring.openapi.v3.httporacle.invalidlocation.fullpath |
| 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.ResponseEntity |
| 7 | +import org.springframework.web.bind.annotation.GetMapping |
| 8 | +import org.springframework.web.bind.annotation.PathVariable |
| 9 | +import org.springframework.web.bind.annotation.PutMapping |
| 10 | +import org.springframework.web.bind.annotation.RequestMapping |
| 11 | +import org.springframework.web.bind.annotation.RestController |
| 12 | +import org.springframework.web.servlet.support.ServletUriComponentsBuilder |
| 13 | + |
| 14 | +@SpringBootApplication(exclude = [SecurityAutoConfiguration::class]) |
| 15 | +@RequestMapping(path = ["/api/resources"]) |
| 16 | +@RestController |
| 17 | +open class HttpInvalidLocationFullPathApplication { |
| 18 | + |
| 19 | + companion object { |
| 20 | + @JvmStatic |
| 21 | + fun main(args: Array<String>) { |
| 22 | + SpringApplication.run(HttpInvalidLocationFullPathApplication::class.java, *args) |
| 23 | + } |
| 24 | + |
| 25 | + private val data = mutableMapOf<Int, String>() |
| 26 | + |
| 27 | + fun reset(){ |
| 28 | + data.clear() |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + |
| 33 | + @GetMapping(path = ["/{id}"]) |
| 34 | + open fun get(@PathVariable("id") id: Int): ResponseEntity<String> { |
| 35 | + val value = data[id] ?: return ResponseEntity.status(404).build() |
| 36 | + return ResponseEntity.status(200).body(value) |
| 37 | + } |
| 38 | + |
| 39 | + |
| 40 | + @PutMapping(path = ["/{id}"]) |
| 41 | + open fun put( |
| 42 | + @PathVariable("id") id: Int |
| 43 | + ): ResponseEntity<Any> { |
| 44 | + |
| 45 | + val isNew = !data.containsKey(id) |
| 46 | + data[id] = "$id" |
| 47 | + |
| 48 | + val location = ServletUriComponentsBuilder |
| 49 | + .fromCurrentContextPath() |
| 50 | + .path("/api/resources/{id}") |
| 51 | + .buildAndExpand(id + 1000) |
| 52 | + .toUri() |
| 53 | + |
| 54 | + val status = if (isNew) 201 else 200 |
| 55 | + return ResponseEntity.status(status) |
| 56 | + .header("Location", location.toString()) |
| 57 | + .build() |
| 58 | + } |
| 59 | +} |
0 commit comments