Solucionado (ver solução)
Solucionado
(ver solução)
4
respostas

Error creating bean with name

Estou tentando salvar meus arquivos igual foi mostrado na aula, peguei meu projeto, e coloquei todos os passos desde configurar com a classe AmazonConfiguration.java e fiz uma classe FileSaver.java tambem, mas quando executo o projeto, vem a seguite Exception:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'fileSaver': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.amazonaws.services.s3.AmazonS3 br.fk.admin.util.FileSaver.amazonS3; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'amazonS3' defined in br.fk.admin.util.AmazonConfiguration: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.amazonaws.services.s3.AmazonS3]: Factory method 'amazonS3' threw exception; nested exception is java.lang.NoSuchMethodError: com.fasterxml.jackson.databind.ObjectMapper.enable([Lcom/fasterxml/jackson/core/JsonParser$Feature;)Lcom/fasterxml/jackson/databind/ObjectMapper;

e logo abaixo:

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.amazonaws.services.s3.AmazonS3 br.fk.admin.util.FileSaver.amazonS3; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'amazonS3' defined in br.fk.admin.util.AmazonConfiguration: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.amazonaws.services.s3.AmazonS3]: Factory method 'amazonS3' threw exception; nested exception is java.lang.NoSuchMethodError: com.fasterxml.jackson.databind.ObjectMapper.enable([Lcom/fasterxml/jackson/core/JsonParser$Feature;)Lcom/fasterxml/jackson/databind/ObjectMapper;
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'amazonS3' defined in br.fk.admin.util.AmazonConfiguration: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.amazonaws.services.s3.AmazonS3]: Factory method 'amazonS3' threw exception; nested exception is java.lang.NoSuchMethodError: com.fasterxml.jackson.databind.ObjectMapper.enable([Lcom/fasterxml/jackson/core/JsonParser$Feature;)Lcom/fasterxml/jackson/databind/ObjectMapper;
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.amazonaws.services.s3.AmazonS3]: Factory method 'amazonS3' threw exception; nested exception is java.lang.NoSuchMethodError: com.fasterxml.jackson.databind.ObjectMapper.enable([Lcom/fasterxml/jackson/core/JsonParser$Feature;)Lcom/fasterxml/jackson/databind/ObjectMapper;
Caused by: java.lang.NoSuchMethodError: com.fasterxml.jackson.databind.ObjectMapper.enable([Lcom/fasterxml/jackson/core/JsonParser$Feature;)Lcom/fasterxml/jackson/databind/ObjectMapper;
4 respostas

minha classe AmazonConfiguration:

package br.fk.admin.util;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;

@Configuration
public class AmazonConfiguration {
    private static final String ACCESS_KEY = "[ACCESS KEY]";
    private static final String SECRET_KEY = "[SECRET KEY]";
    private static final String REGION = "us-east-1";

    @Bean
    public BasicAWSCredentials basicAWSCredentials() {
        return new BasicAWSCredentials(ACCESS_KEY, SECRET_KEY);
    }

    @Bean
    public AmazonS3 amazonS3() {
        return AmazonS3ClientBuilder.standard().withRegion(REGION)
                .withCredentials(new AWSStaticCredentialsProvider(basicAWSCredentials())).build();
    }

}

e minha classe FileSaver:

package br.fk.admin.util;

import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.PutObjectRequest;

@Component
public class FileSaver {

    @Autowired
    private AmazonS3 amazonS3;

    private static final String BUCKET = "tfc-kaique-fernanda";

    public String write(MultipartFile file) {
        try {

            amazonS3.putObject(new PutObjectRequest(BUCKET, file.getOriginalFilename(), file.getInputStream(), null)
                    .withCannedAcl(CannedAccessControlList.PublicRead));
            return "http://s3.amazonaws.com/" + BUCKET + "/" + file.getOriginalFilename();
        } catch (IllegalStateException | IOException e) {
            throw new RuntimeException(e);
        }
    }

}

Tem isso aqui na exception => java.lang.NoSuchMethodError: com.fasterxml.jackson.databind.ObjectMapper.enable([Lcom/fasterxml/jackson/core/JsonParser$Feature;)Lcom/fasterxml/jackson/databind/ObjectMapper;

Meu chute é que está rolando uma incompatibilidade na lib do jackson que você definiu . Conferiu se todas as versões estão iguais as sugeridas pelo curso?

solução!

pesquisei mais um pouco e vi q como minha aplicação é configurada em xml, eu preciso colocar a configuração da amazon no xml tambem, mas agora tenho outro erro:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'regionProvider': Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.cloud.aws.core.region.StaticRegionProvider]: Constructor threw exception; nested exception is java.lang.NoSuchMethodError: com.fasterxml.jackson.databind.ObjectMapper.enable([Lcom/fasterxml/jackson/core/JsonParser$Feature;)Lcom/fasterxml/jackson/databind/ObjectMapper;

meu applicationContext.xml ficou assim:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aws-context="http://www.springframework.org/schema/cloud/aws/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.2.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.8.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
        http://www.springframework.org/schema/cloud/aws/context
        http://www.springframework.org/schema/cloud/spring-cloud-aws-context.xsd">


    <aws-context:context-region
        region="us-east-1" />

    <aws-context:context-credentials>
        <aws-context:simple-credentials
            access-key="[Minha Access Key]"
            secret-key="[Minha secret key]" />
        <aws-context:instance-profile-credentials />
    </aws-context:context-credentials>

    <context:component-scan base-package="br.fk">
        <context:exclude-filter type="annotation"
            expression="org.springframework.stereotype.Controller" />
        <context:exclude-filter type="annotation"
            expression="org.springframework.context.annotation.Configuration" />
    </context:component-scan>

    <jdbc:embedded-database type="HSQL"
        id="dataSource"></jdbc:embedded-database>

    <bean class="org.springframework.orm.jpa.JpaTransactionManager"
        id="transactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <bean id="emf"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="packagesToScan" value="br.fk" />
        <property name="dataSource" ref="dataSource"></property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.format_sql">org.hibernate.dialect.MySQLDialect</prop>
            </props>
        </property>
        <property name="persistenceProvider">
            <bean class="org.hibernate.jpa.HibernatePersistenceProvider" />
        </property>
    </bean>

    <tx:annotation-driven
        transaction-manager="transactionManager" />

    <jpa:repositories base-package="br.fk"
        entity-manager-factory-ref="emf"></jpa:repositories>

    <import resource="security.xml" />

    <beans profile="dev">
        <import resource="database-dev.xml" />
    </beans>

    <beans profile="pre-prod">
        <import resource="database-pre-prod.xml" />

    </beans>
    <beans profile="prod">
        <import resource="database-prod.xml" />
    </beans>

</beans>

Acho que o problema não é esse, a exception continua a mesma. Ainda acho que o problema é na versão do jackson, investigaria por aí.