Spring Boot include ID field in json

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Spring Boot include ID field in json



Propably it's very simple but everything what I found is related to Spring Data Rest. I'm using "spring-boot-starter-web" and I can't found any solution...



Here is my entity clas:


@Entity
@Table(name = "buses")
class Bus(
@Id
@JsonProperty("sideNumber")
@JsonInclude
private val sideNumber: Int,
@NotBlank
var longitude: Double,
@NotBlank
var latitude: Double
)



I tried to annotate sideNumber(Id) property with @JsonInclude and @Jsonproperty but with no luck. In my json reponse I only get longitude and latitude...



My controller looks like that.


@RestController
@RequestMapping("/api")
class BusController {

@Autowired
private lateinit var busRepository: BusRepository

@GetMapping("/buses")
fun getAllBuses(): List<Bus> {
return busRepository.findAll()
}

@PostMapping("/buses")
fun createBus(@Valid @RequestBody bus: Bus): Bus {
return busRepository.save(bus)
}

@GetMapping("/buses/{sideNumber}")
fun getBusById(@PathVariable(value = "sideNumber") sideNumber: Long): Bus {
return busRepository.findById(sideNumber)
.orElseThrow { ResourceNotFoundException("Bus", "sideNumber", sideNumber) }
}

@PutMapping("/buses/{sideNumber}")
fun updateBus(@PathVariable(value = "sideNumber") sideNumber: Long,
@Valid @RequestBody newBus: Bus): Bus {

val bus = busRepository.findById(sideNumber)
.orElseThrow { ResourceNotFoundException("Bus", "sideNumber", sideNumber) }

bus.latitude = newBus.latitude
bus.longitude = newBus.longitude

return busRepository.save(bus)
}

@DeleteMapping("/buses/{sideNumber}")
fun deleteBus(@PathVariable(value = "sideNumber") sideNumber: Long): ResponseEntity<*> {
val bus = busRepository.findById(sideNumber)
.orElseThrow { ResourceNotFoundException("Bus", "sideNumber", sideNumber) }

busRepository.delete(bus)

return ResponseEntity.ok().build<Any>()
}
}



It's nothing complicated though. What I have to do to include this sideNumber(id) propety in json response?? Maybe there is some other good practice to achieve that?



Thanks for any help!









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Makefile test if variable is not empty

Will Oldham

'Series' object is not callable Error / Statsmodels illegal variable name