Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot validation message not shown

I have a Spring Boot application (with version 2.4.5) as a Kotlin project. Now when I enter something invalid, I get an error message, but not my error message that I set in the annotation.

Controller

  @PostMapping(value = ["/seatMap/save"])
    fun addSeatPlan(@RequestPart @Valid data: DSeatMap, @RequestPart image: MultipartFile?, auth: AuthenticationToken) {
        try {
            if (data.uuid == null) {
                seatService.addSeatMap(auth.organisation!!, data, image)
            } else {
                seatService.updateSeatMap(data, auth.organisation!!, image)
            }
        } catch (e: UnauthorizedException) {
            throw e
        }
    }

Data class

import java.util.*
import javax.validation.constraints.NotEmpty

data class DSeatMap(
    var uuid: UUID?,
    @field:NotEmpty(message = "name is empty")
    val name: String,
    var data: String,
    val quantity: Int,
    var settings: DSettings?
)

My Response, here should be message = "name is empty"

{
  "timestamp": "2021-04-22T19:47:08.194+00:00",
  "status": 400,
  "error": "Bad Request",
  "message": "Validation failed for object='data'. Error count: 1",
}

If I set the property it shows me the correct one but I don't want to have all the side information I only want to output the message

server.error.include-binding-errors=always

Result:

{
  "timestamp": "2021-04-22T19:56:30.058+00:00",
  "status": 400,
  "error": "Bad Request",
  "message": "Validation failed for object='data'. Error count: 1",
  "errors": [
    {
      "codes": [
        "NotEmpty.data.name",
        "NotEmpty.name",
        "NotEmpty.java.lang.String",
        "NotEmpty"
      ],
      "arguments": [
        {
          "codes": [
            "data.name",
            "name"
          ],
          "arguments": null,
          "defaultMessage": "name",
          "code": "name"
        }
      ],
      "defaultMessage": "name is empty",
      "objectName": "data",
      "field": "name",
      "rejectedValue": "",
      "bindingFailure": false,
      "code": "NotEmpty"
    }
  ],
  "path": "/api/dashboard/seatMap/save"
}
like image 967
Yunz Avatar asked May 17 '26 11:05

Yunz


1 Answers

@Yunz I think the "errors" attribute gets added to your response since you set server.error.include-binding-errors=always can you try setting that to never or don't define this property and rely on default(which is never).

you need to set the server.error.include-message=always since version 2.3, Spring Boot hides the message field in the response to avoid leaking sensitive information; we can use this property with an always value to enable it

for details check out the spring boot documentation:

like image 55
sham Avatar answered May 19 '26 00:05

sham