Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring引导REST API-补丁-“;java.util.LinkedHashMap不能强制转换为xxx“;_Java_Spring Boot_Rest_Jackson - Fatal编程技术网

Java Spring引导REST API-补丁-“;java.util.LinkedHashMap不能强制转换为xxx“;

Java Spring引导REST API-补丁-“;java.util.LinkedHashMap不能强制转换为xxx“;,java,spring-boot,rest,jackson,Java,Spring Boot,Rest,Jackson,我有两个实体-Concert和ConcertDetail以一对一单向关系连接(一个Concert有一个细节)。 @实体 @表(name=“concert”) 公开课音乐会{ @身份证 @GeneratedValue(策略=GenerationType.IDENTITY) @列(name=“id”) 私有int-id; @列(name=“artist”) 私人弦乐演奏家; @列(name=“city”) 私人城市; @列(name=“place”) 私人弦乐场; @OneToOne(级联=级联类


我有两个实体-Concert和ConcertDetail以一对一单向关系连接(一个Concert有一个细节)。

@实体
@表(name=“concert”)
公开课音乐会{
@身份证
@GeneratedValue(策略=GenerationType.IDENTITY)
@列(name=“id”)
私有int-id;
@列(name=“artist”)
私人弦乐演奏家;
@列(name=“city”)
私人城市;
@列(name=“place”)
私人弦乐场;
@OneToOne(级联=级联类型.ALL)
@JoinColumn(name=“concert\u detail\u id”)
私人音乐会详情;
公开音乐会(){
}
公共音乐会(弦乐演奏家、,
字符串城市,
(位置){
这个艺术家=艺术家;
this.city=城市;
这个地方=地方;
}
//跳过二传手和接球手

@实体
@表(name=“concert\u detail”)
公开课音乐会详情{
@身份证
@GeneratedValue(策略=GenerationType.IDENTITY)
@列(name=“id”)
私有int-id;
@列(name=“description”)
私有字符串描述;
@列(name=“date”)
私有本地日期;
@列(name=“time”)
私有本地时间;
公开音乐会详情(){
}
公共详细信息(字符串描述,
LocalDate日期,
本地时间){
this.description=描述;
this.date=日期;
这个时间=时间;
}
//跳过二传手和接球手
不起作用的是使用
@PatchMapping

@PatchMapping(“/concerts/{id}”)
public ResponseEntity updatePartOfConcert(@RequestBody映射更新,
@路径变量(“id”)int id){
Concert Concert=concertRepository.findById(id);
如果(concert==null){
System.out.println(“在系统中找不到id为“+id+”的Concert!”);
返回新的ResponseEntity(未找到HttpStatus.NOT_);
}
partialUpdate(音乐会、更新);
返回新的响应属性(HttpStatus.NO_内容);
}
私人void partialUpdate(音乐会、地图更新){
if(updates.containsKey(“艺术家”)){
concert.setArtist((字符串)updates.get(“艺术家”);
}
如果(更新.containsKey(“城市”)){
concert.setCity((字符串)updates.get(“city”);
}
如果(更新.containsKey(“地点”)){
concert.setPlace((字符串)updates.get(“place”);
}
if(updates.containsKey(“concertDetail”)){
concert.setConcertDetail((ConcertDetail)updates.get(“ConcertDetail”);
}
concertRepository.save(concert);
}
如果我想更新像艺术家,城市或地方的领域-它的作品很好。 但是,当我将concertDetail作为JSON传递以更新给定的Concert时,我得到以下异常:

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]
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);
    }
@PatchMapping(“/concerts/{id}”)
public ResponseEntity updatePartOfConcert(@RequestBody映射更新,
@路径变量(“id”)int id){
Concert Concert=concertRepository.findById(id);
如果(concert==null){
System.out.println(“在系统中找不到id为“+id+”的Concert!”);
返回新的ResponseEntity(未找到HttpStatus.NOT_);
}
partialUpdate(音乐会、更新);
返回新的响应属性(HttpStatus.NO_内容);
}
私人void partialUpdate(音乐会、地图更新){
if(updates.containsKey(“艺术家”)){
concert.setArtist((字符串)updates.get(“艺术家”);
}
如果(更新.containsKey(“城市”)){
concert.setCity((字符串)updates.get(“city”);
}
如果(更新.containsKey(“地点”)){
concert.setPlace((字符串)updates.get(“place”);
}
if(updates.containsKey(“concertDetail”)){
concert.setConcertDetail((ConcertDetail)updates.get(“ConcertDetail”);
}
concertRepository.save(concert);
}
我传入的JSON邮递员:


我已经尝试了您的控制器方法
updatePartnerTofconcert
采用了
Map
参数,因此您没有告诉Spring要将其反序列化为哪种类型的对象。因此,它也无法反序列化子属性:序列化上面我看到的
concertDetail
对象是否一定正确n您对
ConcertDetail
对象的JSON响应?这三个字段是否还有其他可能的类可以反序列化为其实例

Spring无法知道这一点,因此
updates.get(“concertDetail”)
将是一个
映射,其中包含更新的concert details字段

如果您总是一次性更新整个
ConcertDetail
实例,您可以考虑使用Jackson
ObjectMapper
Map
转换为
ConcertDetail
对象。请参阅示例

但是,如果您只想更新concert detail的某些字段(考虑到您使用的是补丁请求,您可以这样做),那么您必须编写一个单独的方法来更新该字段:

private void partialUpdateConcertDetail(ConcertDetail ConcertDetail,地图细节更新){
if(detailUpdates.containsKey(“说明”)){
setDescription((字符串)detailUpdates.get(“description”);
}
//…对其他字段重复此操作。
}
然后从
partialUpdate
方法调用此方法:

if(updates.containsKey(“concertDetail”)){
partialUpdateConcertDetail(concert.getConcertDetail(),(Map)updates.get(“concertDetail”);
}

谢谢,这确实解决了我的问题。在