简体   繁体   English

post请求用springboot json格式

[英]Post request with springboot json format

I have a product table, I have a second option table.我有一个产品表,我有第二个选项表。 I cannot manage to create my options for the product at the same time as I create the product.我无法在创建产品的同时为产品创建选项。 I tried to create the options individually by creating an option table and a category join table.我试图通过创建选项表和类别连接表来单独创建选项。 When I send the options in json format it doesn't work.当我以 json 格式发送选项时,它不起作用。 I get the bad request error and in the console:我收到错误的请求错误并在控制台中:

JSON parse error: Cannot construct instance of com.pastrycertified.cda.dto.OptionsDto (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value('pie'); JSON 解析错误:无法构造com.pastrycertified.cda.dto.OptionsDto的实例(尽管至少存在一个 Creator):没有要从 String value('pie') 反序列化的字符串参数构造函数/工厂方法; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of com.pastrycertified.cda.dto.OptionsDto (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('pie') at [Source: (org.springframework.util.StreamUtils$NonClosingInputStream);嵌套异常是 com.fasterxml.jackson.databind.exc.MismatchedInputException:无法构造com.pastrycertified.cda.dto.OptionsDto的实例(尽管至少存在一个 Creator):没有字符串参数构造函数/工厂方法可从字符串值反序列化('馅饼')在 [来源:(org.springframework.util.StreamUtils$NonClosingInputStream); line: 2, column: 19] (through reference chain: java.util.LinkedHashMap["typeOption"])]行:2,列:19](通过引用链:java.util.LinkedHashMap["typeOption"])]

Thank you for help谢谢你的帮助

data数据

{
    "typeOption": "product",
    "ingredients": {
        "option1": "test",
        "option2":"test1"
    }
}

controller option controller 选项

@RestController
@RequestMapping("/options")
@RequiredArgsConstructor
public class OptionsController {

    private final OptionsService optionsService;

    @PostMapping("/")
    public void save(
            @RequestBody Map<String, OptionsDto > options
    ) {
        return ResponseEntity.ok(optionsService.save(options));
    }
}

optionService选项服务

public interface OptionsService {

    Options save(OptionsDto options);
}

optionServiceImpl optionServiceImpl

@Service
@RequiredArgsConstructor
public class OptionsServiceImpl implements OptionsService {


    @Override
    public Options save(OptionsDto options) {

        Options option = OptionsDto.toEntity(options);
        option.setTypeOption(option.getTypeOption());
        option.setIngredients(option.getIngredients());

        return option;
    }
}

optionDto选项Dto

@Getter
@Setter
@AllArgsConstructor
@Builder
public class OptionsDto {

    private Integer id;

    private String typeOption;

    private String ingredients;

    private String nameCategory;

    private CategoryDto category;

    public static OptionsDto fromEntity(Options options) {

        return OptionsDto.builder()
                .id(options.getId())
                .typeOption(options.getTypeOption())
                .ingredients(options.getIngredients())
                .nameCategory(options.getCategory().getName())
                .build();
    }

    public static Options toEntity(OptionsDto options) {

        return Options.builder()
                .id(options.getId())
                .typeOption(options.getTypeOption())
                .ingredients(options.getIngredients())
                .build();
    }
}

As Jens mentioned, you need a default constructor in the OptionDto class. Also, you must decide whether ingredients is a String or a Map.正如 Jens 提到的,您需要 OptionD 中的默认构造函数 class。此外,您必须确定ingredients是字符串还是 Map。

In the controller, you are asking for a Map<> but what you pass in the JSON is not a map. Your controller must be asking for an OptionsDto and not a Map.在 controller 中,您要求的是 Map<> 但您在 JSON 中传递的不是 map。您的 controller 必须要求 OptionsDto 而不是 Map。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM