How to transform a JSON input into an Entity

Hi!

I have this method in my controller that is supposed to create a new entity:


@RestController
@RequestMapping("/settings")
public class SettingController {

    private final SettingService settingService;
    private final EntitySerializationAPI entitySerializationAPI;
    private final MetaClass metaClass;

    @Inject
    public SettingController(SettingService settingService,
                             EntitySerializationAPI entitySerializationAPI, 
                             MetaClass metaClass) {
        this.settingService = settingService;
        this.entitySerializationAPI = entitySerializationAPI;
        this.metaClass = metaClass;
    }


    @PostMapping("/")
    public ResponseEntity<String> addSetting(@RequestBody String settingInput,
                                              HttpServletRequest request) {

                Setting settingInstance = entitySerializationAPI.entityFromJson(settingInput, metaClass, EntitySerializationOption.PRETTY_PRINT);

        
        Setting setting = settingService.createSetting(settingInstance);

        UriComponents uriComponents = UriComponentsBuilder.fromHttpUrl(request.getRequestURL().toString())
                .path("/{id}")
                .buildAndExpand(setting.getId().toString());

        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setLocation(uriComponents.toUri());
        return new ResponseEntity<>(entitySerializationAPI.toJson(setting), httpHeaders, HttpStatus.CREATED);

    }
}

The problem is in this line because the Injection cannot resolve this metaClass bean:


Setting settingInstance = entitySerializationAPI.entityFromJson(settingInput, metaClass, EntitySerializationOption.PRETTY_PRINT);

Error:


Unsatisfied dependency expressed through constructor parameter 2; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.haulmont.chile.core.model.MetaClass' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

So how do I convert this JSON from a request into an Entity ?

Thanks in advance!

James

Hi James,
In fact, MetaClass is not a bean, so it simply cannot be injected. If you dive into the method you will find javadocs saying:

   /**
     * Deserializes a JSON object to the entity.
     * <p>
     * The {@code metaClass} parameter defines a result entity metaClass. It is optional. It must be defined if the JSON
     * object doesn't contain an "_entityName" property.
     * <p>
     * An entity may be serialized to the JSON in slightly different formats. The format is defined by the {@code
     * options} parameter. See {@link EntitySerializationOption} for details.
     *
     * @param json      a string that represents a JSON object
     * @param metaClass a metaClass of the entity that will be created
     * @param options   options specifying how a JSON object graph was serialized
     * @return an entity
     */

So, you can add the _entityName attribute to the JSON body, or get a metaclass by name injecting the Metadata bean and calling the Metadata#getClass(String name) method.

It has worked fine now

Thanks Aleksey!!

A post was split to a new topic: Attribute that will eventually be stored into a JsonB column