Home > front end >  Error creating bean when running .JAR file
Error creating bean when running .JAR file

Time:10-03

I have a Java application with Spring boot, built with MySQL database and Spring security. The application's purpose is to connect to SOAP API and using Thymleaf's front-end to interact with the data.

The project works just fine when running it on the localhost with IntelliJ IDEA, with all the SOAP connections. The fat jar was built successfully when running mvn clean package. I would like to deploy the jar file to a hosting server.

Any help is appreciated.

But when running it using java -jar <your_app_name>-<your_app_version>.jar it crashes and give the following error:

Error

        2021-10-02 12:47:58.149  WARN 32908 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: o
        rg.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'authController' defined in URL [jar:file:/C:/Users/amerm/Downloads/Compressed/project_name/target/project_name-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/com/project_name/PersonDetailsClient/Controller/AuthController.class]: Unsatisfied dependency expressed through constr
        uctor parameter 2; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'personDetailsServiceImpl' defined in URL [jar:fi
        le:/C:/Users/amerm/Downloads/Compressed/project_name/target/project_name-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/com/project_name/PersonDetailsClient/Service/ServiceImpl/PersonDetai
        lsServiceImpl.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean
        with name 'personDetailsClient' defined in class path resource [com/project_name/PersonDetailsClient/Config/PersonDetailsClientConfiguration.class]: Bean instantiation via fact
        ory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.project.name.PersonDetailsClient.PersonDetailsClient]: Fa
        ctory method 'personDetailsClient' threw exception; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.ws.soap.saaj.Sa
        ajSoapMessageFactory]: Unresolvable class definition; nested exception is java.lang.NoClassDefFoundError: javax/xml/soap/SOAPException

AuthController

@Controller
@RequestMapping(value = "/")
public class AuthController {

    @Autowired
    public AuthController(UserService userService, RoleDao roleDao, IPersonDetailsService personDetailsService) {
        this.personDetailsService = personDetailsService;
        this.userService = userService;
        this.roleDao = roleDao;
    }

    private IPersonDetailsService personDetailsService;

    private UserService userService;

    private RoleDao roleDao;

    @RequestMapping
    public String home(){
        return "redirect:/index";
    }

    @RequestMapping("index")
    public String index(){
        return "index";
    }

    @RequestMapping(value = "signup", method = RequestMethod.GET)
    public String signup(Model model) {
        PersonDetails personDetails = new PersonDetails();

        model.addAttribute("user", personDetails);

        return "signup";
    }

    @RequestMapping(value = "signup", method = RequestMethod.POST)
    public String signupPost(@ModelAttribute("user")  PersonDetails newPersonDetails, Model model) throws DatatypeConfigurationException {

        if (userService.checkUserExists(newPersonDetails.getUsername(), newPersonDetails.getEmailAddress())) {

            if (userService.checkEmailExists(newPersonDetails.getEmailAddress())) {
                model.addAttribute("emailExists", true);
            }

            if (userService.checkUsernameExists(newPersonDetails.getUsername())) {
                model.addAttribute("usernameExists", true);
            }

            return "signup";
        } else {
            Set<UserRole> userRoles = new HashSet<>();
            userRoles.add(new UserRole(newPersonDetails, roleDao.findByName("ROLE_USER")));

            String personId = personDetailsService.findPersonByVariant1(newPersonDetails.getLastName(), newPersonDetails.getInitials(), newPersonDetails.getDateOfBirth()).getPersonDetails().getPersonId();
            userService.createUser(newPersonDetails, userRoles);
            PersonDetails user = userService.findByUsername(newPersonDetails.getUsername());
            user.setPersonId(personId);
            userService.saveUser(user);
            if (personId == null){
                String personIdCreated = personDetailsService.createPersonByVariant1(newPersonDetails.getLastName(), newPersonDetails.getInitials(), newPersonDetails.getDateOfBirth()).getPersonDetails().getPersonId();
                userService.createUser(newPersonDetails, userRoles);
                PersonDetails person = userService.findByUsername(newPersonDetails.getUsername());
                person.setPersonId(personIdCreated);
                userService.saveUser(newPersonDetails);
            }

            return "redirect:/";
        }
    }
}

PersonDetailsServiceImpl

@Service
public class PersonDetailsServiceImpl implements IPersonDetailsService {

    final String PersonDetailsEndPoint = "ENDPOINT_URL";
    final String userId = "NUM";
    final String organisationToken = "NUM";

    PersonDetailsClient personDetailsClient;

    @Autowired
    public PersonDetailsServiceImpl(PersonDetailsClient personDetailsClient) {
        this.personDetailsClient = personDetailsClient;
    }

    @Override
    public GetPersonDetailsResponse findPersonByVariant1(String lastName, String initials, Date dateOfBirth) throws DatatypeConfigurationException {

        ObjectFactory objectFactory = new ObjectFactory();
        GetPersonDetailsRequest.PersonDetails person = objectFactory.createGetPersonDetailsRequestPersonDetails();
        person.setLastName(lastName);
        person.setInitials(initials);
        GregorianCalendar bgc = new GregorianCalendar();
        bgc.setTime(dateOfBirth);
        XMLGregorianCalendar bDate = DatatypeFactory.newInstance().newXMLGregorianCalendar(bgc);
        person.setDateOfBirth(bDate);

        GetPersonDetailsRequest getPersonDetailsRequest = objectFactory.createGetPersonDetailsRequest();
        getPersonDetailsRequest.setPersonDetails(person);

        GetPersonDetailsResponse getPersonDetailsResponse = personDetailsClient.getPersonDetailsResponse(PersonDetailsEndPoint, getPersonDetailsRequest, new TokenHeaderRequestCallback(userId, organisationToken));

        return getPersonDetailsResponse;
    }

    @Override
    public CreatePersonDetailsResponse createPersonByVariant1(String lastName, String initials, Date dateOfBirth) throws DatatypeConfigurationException {

        try {
            ObjectFactory objectFactory = new ObjectFactory();
            CreatePersonDetailsRequest.PersonDetails person = objectFactory.createCreatePersonDetailsRequestPersonDetails();
            person.setLastName(lastName);
            person.setInitials(initials);
            GregorianCalendar bgc = new GregorianCalendar();
            bgc.setTime(dateOfBirth);
            XMLGregorianCalendar bDate = DatatypeFactory.newInstance().newXMLGregorianCalendar(bgc);
            person.setDateOfBirth(bDate);


            CreatePersonDetailsRequest createPersonDetailsRequest = objectFactory.createCreatePersonDetailsRequest();
            createPersonDetailsRequest.setPersonDetails(person);

            CreatePersonDetailsResponse createPersonDetailsResponse = personDetailsClient.createPersonDetailsResponse(PersonDetailsEndPoint, createPersonDetailsRequest, new TokenHeaderRequestCallback(userId, organisationToken));

            return createPersonDetailsResponse;

        } catch (SoapFaultClientException soapFaultClientException) {
            return null;
        }
    }


    @Override
    public CheckChangesPersonDetailsResponse checkPersonChanges(String personId, String title, String lastName, String initials, String firstName, String infix, Date dateOfBirth, String streetName, String houseNumber, String houseNumberAddition, String postalCode, String residence, String country, String countryOfBirth, String lastNameOfBirth, String infixOfBirth, String nationality, GenderType gender, String insurancePolicy, String policyNumber, String socialSecurityNumber, Date dateOfDeath, String telephone, String emailAddress) throws DatatypeConfigurationException {
...
    }

    @Override
    public UpdatePersonDetailsResponse updatePersonDetails(String personId, String title, String lastName, String initials, String firstName, String infix, Date dateOfBirth, String streetName, String houseNumber, String houseNumberAddition, String postalCode, String residence, String country, String countryOfBirth, String lastNameOfBirth, String infixOfBirth, String nationality, GenderType gender, String insurancePolicy, String policyNumber, String socialSecurityNumber, Date dateOfDeath, String telephone, String emailAddress) throws DatatypeConfigurationException {
...
    }
}

PersonDetailsClientConfiguration

@Configuration
public class PersonDetailsClientConfiguration {

    @Bean
    public Jaxb2Marshaller marshaller() throws Exception {

        Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
        marshaller.setContextPath("com.<project_name>.stub");
        marshaller.afterPropertiesSet();

        return marshaller;
    }

    @Bean
    public WebServiceTemplate webServiceTemplate(Jaxb2Marshaller marshaller) {

        WebServiceTemplate webServiceTemplate = new WebServiceTemplate();
        webServiceTemplate.setDefaultUri("ENDPOINT_URL");
        webServiceTemplate.setMarshaller(marshaller);
        webServiceTemplate.setUnmarshaller(marshaller);

        return webServiceTemplate;
    }

    @Bean
    public PersonDetailsClient personDetailsClient(Jaxb2Marshaller marshaller) {

        PersonDetailsClient personDetailsClient = new PersonDetailsClient();
        personDetailsClient.setDefaultUri("ENDPOINT_URL");
        personDetailsClient.setMarshaller(marshaller);
        personDetailsClient.setUnmarshaller(marshaller);

        return personDetailsClient;
    }
}

PersonDetailsClient

public class PersonDetailsClient extends WebServiceGatewaySupport {

    public GetPersonDetailsResponse getPersonDetailsResponse(String personDetailsEndPoint, GetPersonDetailsRequest getPersonDetailsRequest, TokenHeaderRequestCallback tokenHeaderRequestCallback) {

        GetPersonDetailsResponse getPersonDetailsResponse = (GetPersonDetailsResponse) getWebServiceTemplate().marshalSendAndReceive(personDetailsEndPoint, getPersonDetailsRequest, tokenHeaderRequestCallback);

        return getPersonDetailsResponse;
    }

    public CreatePersonDetailsResponse createPersonDetailsResponse (String personDetailsEndPoint, CreatePersonDetailsRequest createPersonDetailsRequest, TokenHeaderRequestCallback tokenHeaderRequestCallback) {

        CreatePersonDetailsResponse createPersonDetailsResponse = (CreatePersonDetailsResponse) getWebServiceTemplate().marshalSendAndReceive(personDetailsEndPoint, createPersonDetailsRequest, tokenHeaderRequestCallback);

        return createPersonDetailsResponse;
    }

    public CheckChangesPersonDetailsResponse checkChangesPersonDetailsResponse (String personDetailsEndPoint, CheckChangesPersonDetailsRequest checkChangesPersonDetailsRequest, TokenHeaderRequestCallback tokenHeaderRequestCallback) {

        CheckChangesPersonDetailsResponse checkChangesPersonDetailsResponse = (CheckChangesPersonDetailsResponse) getWebServiceTemplate().marshalSendAndReceive(personDetailsEndPoint, checkChangesPersonDetailsRequest, tokenHeaderRequestCallback);

        return checkChangesPersonDetailsResponse;
    }

    public UpdatePersonDetailsResponse updatePersonDetailsResponse (String personDetailsEndPoint, UpdatePersonDetailsRequest updatePersonDetailsRequest, TokenHeaderRequestCallback tokenHeaderRequestCallback) {

        UpdatePersonDetailsResponse updatePersonDetailsResponse = (UpdatePersonDetailsResponse) getWebServiceTemplate().marshalSendAndReceive(personDetailsEndPoint, updatePersonDetailsRequest, tokenHeaderRequestCallback);

        return updatePersonDetailsResponse;
    }
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.<group_name></groupId>
    <artifactId><artifactId></artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name><Project_name></name>
    <properties>
        <java.version>8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-oxm</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.ws</groupId>
            <artifactId>spring-ws-core</artifactId>
        </dependency>

    </dependencies>

    <distributionManagement>
        <snapshotRepository>
            <uniqueVersion>false</uniqueVersion>
            <id>snapshot</id>
            <name>Carewebport Snapshots</name>
            <url>https://repo.maven.apache.org/maven2</url>
            <layout>default</layout>
        </snapshotRepository>
    </distributionManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.jvnet.jaxb2.maven2</groupId>
                <artifactId>maven-jaxb2-plugin</artifactId>
                <version>0.14.0</version>
                <configuration>
                    <schemaLanguage>WSDL</schemaLanguage>
                    <generatePackage>com.<project_name>.stub</generatePackage>
                    <schemas>
                        <schema>
                            <url>URL.WSDL</url>
                        </schema>
                    </schemas>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

CodePudding user response:

This might be caused by the Java version that you're using during runtime. You will have to add the javax ws package as a dependency, since it isn't bundled with newer Java versions anymore. See Java 11 package javax.xml.soap does not exist for more info

CodePudding user response:

1.In sts/eclipse type ctrl t and type the class name. if found then note the jar name from which it is coming let's say x. If not found on classpath then you have to add the dependency where this class is present

2 Open your jar built under target folder with any zip extractor and in WEB-INF/lib folder just check jar x whether javax/xml/soap/SOAPException is found under that x jar or not.

  • Related