I'm using spring-boot-starter-web
and I want to use sideNumber
of the bus as the Id
value and return this value in a JSON repsonse.
Probably it's very simple but everything I found is related to Spring Data Rest.
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 the sideNumber(Id)
property with @JsonInclude
and @Jsonproperty
but with no luck. In my JSON response I only get longitude and latitude.
My controller looks like this:
@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>()
}
}
What do I have to do to include this sideNumber(id)
property into the JSON response?
By declaring a member property private you're telling Kotlin not to generate a getter for it.
Jackson, which is used by SpringBoot, uses getters to serialize objects.
That's the reason it doesn't see your property. And if it doesn't see the property, it doesn't get annotation either.
Easiest way to solve it would be to leave it as a val
, but make it not private: val sideNumber: Int
More complicated way would be to create a @Bean of type ObjectMapper and configure Jackson to serialize also private members, as described here: https://docs.spring.io/spring-boot/docs/current/reference/html/howto-spring-mvc.html#howto-customize-the-jackson-objectmapper
I hope you must be extending RepositoryRestConfigurerAdapter and overrding configureRepositoryRestConfiguration method. If yes then Set the entity class names in exposeIdsFor() method. This will add display the id column in json.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.