Java Spring Boot REST API - Patch - "java.util.LinkedHashMap cannot be cast to xxx"

lvndsky :


I have two entities - Concert and ConcertDetail connected in One-To-One unidirectional relationship (one concert has one detail).
enter image description here

@Entity
@Table(name = "concert")
public class Concert {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private int id;

    @Column(name = "artist")
    private String artist;

    @Column(name = "city")
    private String city;

    @Column(name = "place")
    private String place;

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "concert_detail_id")
    private ConcertDetail concertDetail;

    public Concert() {
    }

    public Concert(String artist,
                   String city,
                   String place) {
        this.artist = artist;
        this.city = city;
        this.place = place;
    }
//skipping the setters and getters

@Entity
@Table(name = "concert_detail")
public class ConcertDetail {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private int id;

    @Column(name = "description")
    private String description;

    @Column(name = "date")
    private LocalDate date;

    @Column(name = "time")
    private LocalTime time;

    public ConcertDetail() {
    }

    public ConcertDetail(String description,
                         LocalDate date,
                         LocalTime time) {
        this.description = description;
        this.date = date;
        this.time = time;
    }
//skipping the setters and getters

What does not work, is the partial update with @PatchMapping.

@PatchMapping("/concerts/{id}")
    public ResponseEntity<Concert> updatePartOfConcert(@RequestBody Map<String,Object> updates,
                                                       @PathVariable("id") int id) {
        Concert concert = concertRepository.findById(id);
        if (concert == null) {
            System.out.println("Concert with id: " + id + " not found in the system!");
            return new ResponseEntity<Concert>(HttpStatus.NOT_FOUND);
        }
        partialUpdate(concert, updates);
        return new ResponseEntity<Concert>(HttpStatus.NO_CONTENT);
    }

    private void partialUpdate(Concert concert, Map<String,Object> updates) {
        if(updates.containsKey("artist")) {
            concert.setArtist((String) updates.get("artist"));
        }
        if(updates.containsKey("city")) {
            concert.setCity((String) updates.get("city"));
        }
        if(updates.containsKey("place")) {
            concert.setPlace((String) updates.get("place"));
        }
        if(updates.containsKey("concertDetail")) {
            concert.setConcertDetail((ConcertDetail) updates.get("concertDetail"));
        }
        concertRepository.save(concert);
    }

If I want to update the fields like artist, city or place - it works fine. But when I pass the concertDetail as JSON to update the given Concert with it - I get the following exception:

java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.musicao.app.entity.ConcertDetail
    at com.musicao.app.restController.ConcertRestController.partialUpdate(ConcertRestController.java:109) ~[classes/:na]
    at com.musicao.app.restController.ConcertRestController.updatePartOfConcert(ConcertRestController.java:94) ~[classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_242]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_242]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_242]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_242]
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) ~[spring-web-5.2.4.RELEASE.jar:5.2.4.RELEASE]
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-5.2.4.RELEASE.jar:5.2.4.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106) ~[spring-webmvc-5.2.4.RELEASE.jar:5.2.4.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:879) ~[spring-webmvc-5.2.4.RELEASE.jar:5.2.4.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793) ~[spring-webmvc-5.2.4.RELEASE.jar:5.2.4.RELEASE]
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.2.4.RELEASE.jar:5.2.4.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040) ~[spring-webmvc-5.2.4.RELEASE.jar:5.2.4.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) ~[spring-webmvc-5.2.4.RELEASE.jar:5.2.4.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.2.4.RELEASE.jar:5.2.4.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:880) ~[spring-webmvc-5.2.4.RELEASE.jar:5.2.4.RELEASE]

My Patch method in RestController:

@PatchMapping("/concerts/{id}")
    public ResponseEntity<Concert> updatePartOfConcert(@RequestBody Map<String,Object> updates,
                                                       @PathVariable("id") int id) {
        Concert concert = concertRepository.findById(id);
        if (concert == null) {
            System.out.println("Concert with id: " + id + " not found in the system!");
            return new ResponseEntity<Concert>(HttpStatus.NOT_FOUND);
        }
        partialUpdate(concert, updates);
        return new ResponseEntity<Concert>(HttpStatus.NO_CONTENT);
    }

    private void partialUpdate(Concert concert, Map<String,Object> updates) {
        if(updates.containsKey("artist")) {
            concert.setArtist((String) updates.get("artist"));
        }
        if(updates.containsKey("city")) {
            concert.setCity((String) updates.get("city"));
        }
        if(updates.containsKey("place")) {
            concert.setPlace((String) updates.get("place"));
        }
        if(updates.containsKey("concertDetail")) {
            concert.setConcertDetail((ConcertDetail) updates.get("concertDetail"));
        }
        concertRepository.save(concert);
    }

JSON I pass in Postman:
enter image description here

I have already tried Spring REST and PATCH method

Luke Woodward :

Your controller method updatePartOfConcert takes a Map<String, Object> parameter, so you're not telling Spring what type of object to deserialize it to. As a result it can't deserialize child properties either: would it necessarily be correct to serialize the concertDetail object seen above in your JSON response to a ConcertDetail object? Are there other possible classes that these three fields could deserialize to an instance of?

Spring can't know this, so updates.get("concertDetail") will be a Map that contains the updated concert-details fields.

If you're always updating the entire ConcertDetail instance in one go, you could look at using a Jackson ObjectMapper to convert the Map into a ConcertDetail object. See for example this question.

However, if you only want to update some of the fields of the concert detail (which you can do, given that you're using a PATCH request), then you'll have to write a separate method to update that:

private void partialUpdateConcertDetail(ConcertDetail concertDetail, Map<String, Object> detailUpdates) {
    if (detailUpdates.containsKey("description")) {
        concertDetail.setDescription((String) detailUpdates.get("description"));
    }

    // ... repeat for other fields.    
}

You then call this method from your partialUpdate method:

    if (updates.containsKey("concertDetail")) {
        partialUpdateConcertDetail(concert.getConcertDetail(), (Map<String, Object>) updates.get("concertDetail"));
    }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=356350&siteId=1