0

I am using Postman and learning how to do a POST request to add a new Item to the cart and write it to the database. The Java code for the item is:

public class Item  {    
    private int id;
    private String name;
    private String description;
    private LocalDate expired;
}

with getters and setters.

In Spring framework I have the next method:

@PostMapping
public void add(@RequestBody Item newItem) {
    itemSer.addNewItem(newItem);
}

Using postman in the body part I selected raw -> json: (its the result of a GET request.)

{
   "id": 2,
   "name": "postman",
   "description": "postman",
   "expired": {
        "year": 1700,
        "month": "NOVEMBER",
        "monthValue": 11,
        "dayOfMonth": 21,
        "leapYear": false,
        "dayOfWeek": "WEDNESDAY",
        "dayOfYear": 325,
        "era": "CE",
        "chronology": {
            "id": "ISO",
            "calendarType": "iso8601",
            "isoBased": true
        }
    }
}

Postman give me an 405 error but I have no idea why, I am using tomcat 9. Is there same problem also for the date format?

EDIT:

So the problem is only on the LocalDate, I have to format in a way that is readable by java with a Post request. Snooping around I did find this solution: How to send Date in REST API in POST method

(if you are using maven)

<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-jsr310</artifactId>
    <version>${jackson.version}</version>
</dependency>

the add in the AppConfig di spring:

@Bean
public ObjectMapper createObjectMapper() {  
    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(new JavaTimeModule());                
    mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
    return mapper;
}

and int the Itmes class over the LocalDate variable:

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
private LocalDate date;

the post is now just looks like the next:

{
    "id": 2,
    "name": "postman",
    "description": "postman",
    "expired": "2030-01-02"
}

I still have a question, reading this i did understand generally how it works, where can I find some other info on how this works? or if someone want to explain I am happy to listen.

Volodya Lombrozo
  • 2,325
  • 2
  • 16
  • 34

1 Answers1

1

You didn't mention it, but it appears you're using Spring framework for working with HTTP and Jackson for JSON handling. Spring framework actually uses Jackson as its default implementation for parsing request and response bodies. Spring docs:

Spring Boot provides integration with three JSON mapping libraries:

  • Gson
  • Jackson
  • JSON-B

Jackson is the preferred and default library.

It means that each time an HTTP request reaches your controller method, Jackson parses the raw HTTP body into an Item object:

@PostMapping
public void add(@RequestBody Item newItem) {
    //newItem have already parsed from JSON by Jackson
    itemSer.addNewItem(newItem);
}

Jackson has a modular design, meaning that it can be extended with modules to provide additional functionality. A module is a collection of serializers, deserializers, and other components that extend Jackson's capabilities. For example, the JavaTimeModule provides support for serializing and deserializing Java 8 date and time classes, such as LocalDate and LocalDateTime.

To use a particular module in Jackson, you simply need to register it with an instance of the ObjectMapper class. This can be done with the registerModule() method, as shown in the example in your question.

Once registered with an ObjectMapper, a module's serializers and deserializers become available for parsing HTTP bodies and you can then use Jackson annotations like @JsonFormat to further customize the serialization and deserialization of specific classes and fields.

Before Java 8, Jackson supported Java 6 and 7, which did not have any built-in date and time classes. So, to handle dates and times, Jackson relied on external libraries, such as Joda Time, or on custom serializers and deserializers that were written by the user.


Volodya Lombrozo
  • 2,325
  • 2
  • 16
  • 34