GithubHelp home page GithubHelp logo

Comments (4)

scottfrederick avatar scottfrederick commented on April 28, 2024 1

This comment on a Spring Framework issue might also be relevant.

from spring-boot.

wilkinsona avatar wilkinsona commented on April 28, 2024

There were some notable changes in this area in Spring Framework 6.1 (spring-projects/spring-framework#29825). The change that you're seeing may be as a result of a refinement of those changes in a Spring Framework patch release. It's hard to be certain with the level of detail that you've currently provided. You could try using spring.version to override the version of Spring Framework that's used to see if this changes the behavior. If it does, then this will have to be investigated by the Spring Framework team. If it does not then we'll need some more information in the form of a minimal sample that we can use to reproduce the behavior you have described.

from spring-boot.

pepavesely avatar pepavesely commented on April 28, 2024

Hi,
@scottfrederick thank you for the link. This issue seems to be duplicate of that one.

@PostMapping("/test1")
ResponseEntity<String> test1(@Valid @RequestBody TestModel testModel) {
    return ResponseEntity.ok().build();
}

throws MethodArgumentNotValidException but

@PostMapping("/test2/{param}")
ResponseEntity<String> test2(
        @NotNull @PathVariable(name = "param") String param,
        @Valid @RequestBody TestModel testModel) {
    return ResponseEntity.ok().build();
}

throws HandlerMethodValidationException

My pom.xml is fairly simple:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.2.4</version>
    <relativePath/>
</parent>

<properties>
    <java.version>17</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-validation</artifactId>
    </dependency>
   <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

TestApplication.class:

package com.example.test;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.Data;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.method.annotation.HandlerMethodValidationException;

@ControllerAdvice
@RestController
@SpringBootApplication
public class TestApplication {

    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args);
    }

    @PostMapping("/test1")
    ResponseEntity<String> test1(@Valid @RequestBody TestModel testModel) {
        return ResponseEntity.ok().build();
    }

    @PostMapping("/test2/{param}")
    ResponseEntity<String> test2(
            @NotNull @PathVariable(name = "param") String param,
            @Valid @RequestBody TestModel testModel) {
        return ResponseEntity.ok().build();
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseEntity<String> handleException(final MethodArgumentNotValidException ex) {
        return ResponseEntity.badRequest().build();
    }

    @ExceptionHandler
    public ResponseEntity<String> handleUncaughtExceptions(final HandlerMethodValidationException ex) {
        return ResponseEntity.badRequest().build();
    }

    @Data
    public static class TestModel {

        @Size(min = 3)
        private String name;
    }
}

In spring-boot-starter-parent version 3.2.1, both cases are handled by MethodArgumentNotValidException

from spring-boot.

wilkinsona avatar wilkinsona commented on April 28, 2024

Thanks, @pepavesely. Closing as a duplicate of spring-projects/spring-framework#32396.

from spring-boot.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.