How to transform a field of the Requestbody before arriving at the handle with annotation

Related searches

I need to make some changes in the body of some payloads in post method to change values ​​like uppercase or lowercase. I've been researching what is closest to what I need is HandlerMethodArgumentResolver

@Documented
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface LowerCase {

}

@Data
@Builder
@Document
@NoArgsConstructor
@AllArgsConstructor
public class Customer extends Model {

  @LowerCase
  private String email;

  @LowerCase
  private String username;
}

@RequestMapping(value = "user", method = RequestMethod.POST)
ResponseEntity<Customer> test(@RequestBody @Valid Customer payload) {
  System.out.println(payload);
  return ResponseEntity.ok(payload);
}

if I send this

{
  "email": "MYMAIL@GMAIL.COM",
  "username": "JIBARRA"
}

The result with need this

{
  "email": "mymail@gmail.com",
  "username": "jibarra"
}

Thanks

@Data
@Builder
@Document
@NoArgsConstructor
@AllArgsConstructor
public class Customer extends Model {

  @JsonDeserialize(using = ToLowerCaseDeserializer.class)
  private String email;

  @JsonDeserialize(using = ToLowerCaseDeserializer.class)
  private String username;
}

In ToLowerCaseDeserializer.java implement the logic to convert the incoming text to lowercase

public class ToLowerCaseDeserializer extends StdDeserializer<String> {

    private static final long serialVersionUID = 7527542687158493910L;

    public ToLowerCaseDeserializer() {
        super(String.class);
    }

    @Override
    public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        return _parseString(p, ctxt).toLowerCase();
    }

}

Spring MVC @RequestBody @ResponseBody Example , If a method parameter is annotated with @RequestBody, Spring will bind the incoming HTTP request body(for the URL mentioned in This is the controller method to handle typical HTTP Post request [for URL /user/create]. In a pure REST But Spring need help to convert the request body into user object. ym_coding The @RequestBody annotation can be used for handling web requests. More specifically, it is used to bind a method parameter with the body of a request and the way it works is HttpMessageConverter converts the request’s body based on the type of the content of the request.

You can make changes to the request body using Spring message converters ,define a custom converter for the Json converter(which converts a JSON to Object) and add it like below for spring-boot.

To write a message converter, extend the Spring AbstractHttpMessageConverter(for a new custom converter), and override the three methods readInternal(),writeInternal() and supports() .

Spring uses HttpMessageConverters to render @ResponseBody (or responses from @RestController). You can contribute additional converters by adding beans of the appropriate type in a Spring Boot context. If a bean you add is of a type that would have been included by default anyway (such as MappingJackson2HttpMessageConverter for JSON conversions), it replaces the default value. A convenience bean of type HttpMessageConverters is provided and is always available if you use the default MVC configuration. It has some useful methods to access the default and user-enhanced message converters (For example, it can be useful if you want to manually inject them into a custom RestTemplate).

@Configuration
public class MyConfiguration {
@Bean
public HttpMessageConverters customConverters() {
    HttpMessageConverter<?> additional = ...
    HttpMessageConverter<?> another = ...
    return new HttpMessageConverters(additional, another);
}

}

Similar post : Spring boot how to custom HttpMessageConverter

Reference for answer : https://docs.spring.io/spring-boot/docs/current/reference/html/howto-spring-mvc.html#howto-customize-the-responsebody-rendering

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-developing-web-applications.html#boot-features-spring-mvc-message-converters

@PostMapping and @RequestBody Example in Spring MVC, @PostMapping to handle HTTP POST Requests Notice how the @ RequestBody annotation is used to mark the Java class into which the incoming JSON will be mapped by Spring Framework. to convert the JSON or XML payload which is in the request body of HTTP Required fields are marked *. In this quick article, we will discuss usage Spring @RequestBody and @ResponseBody annotations with examples. @RequestBody and @ResponseBody annotations are used to bind the HTTP request/response body with a domain object in method parameter or return type. Behind the scenes, these annotation uses HTTP Message converters to convert the body of

Just do it in the controller. I don't think the message converter is the place for converting data

Converting the case implies some sort business constraint or rule. As such, I think you would want that happening explicitly in the code of the controller rather than some property of an annotation. Then way if the requirements change (because they always do), you know where and how to change it.

Spring @RequestBody Annotation Example, Spring @RequestBody Annotation - Model View Controller (MVC) Overview. Fig. this class as a spring controller and is responsible for handling the incoming requests. uses the HTTP message converters to convert the HTTP request body into the domain object Leave this field empty if you're human:� @RequestBody and @ResponseBody annotations are used to bind the HTTP request/response body with a domain object in method parameter or return type. Behind the scenes, these annotation uses HTTP Message converters to convert the body of HTTP request/response to domain objects.

Spring MVC, Before we start, i want to explain the purpose of these annotations. If you annotate a methods parameter with @RequestBody, spring will try to convert the content of the incoming request body to your parameter object on the fly. In this example i've written a converter that handles the conversion of a� import org.springframework.http.MediaType; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestBody; import org

Tutorial, @Entity is a JPA annotation to make this object ready for storage in a JPA-based data store. we can now turn to Spring Data JPA to handle the tedious database interactions. RequestBody; import org.springframework.web.bind.annotation. Before you open up the Employee class and replace the single field name with� @RequestBody will help to convert the body of the HTTP Request to the User object. The method can handle a request body that is in the form of either XML or JSON. The transformed user object will be saved to database using the userRepository. The persisted user object will be retuned by the method.

Right now, these validations don’t really do anything. If you would run the application, it would result in the same output as before. To solve this, we need to add the @Valid annotation to the parameters we’d like to be validated. In my case, that would be the request body itself:

Comments
  • extends StdDeserializer<String> public ToLowerCaseDeserializer() { super(String.class); } @Override public String deserialize(JsonParser p, DeserializationContext context) throws IOException, JsonProcessingException { return _parseString(p, context).toLowerCase(); }
  • It is deserialize not serialize. I have edited my answer and also given the ToLowerCaseDeserializer impl
  • stackoverflow.com/questions/40426150/…
  • thank for reply, this my solution stackoverflow.com/questions/40426150/…
  • stackoverflow.com/questions/40426150/…