'Deployment web app Jakarta EE9 and React on heroku, failed when fetching from backend
I created simple app using jakarta ee 9 and react. I integrated react into jakarta and everything works fine when I deploy on localhost. Problem occured when i deployed war on heroku. React cant fetch data from endpoint.
React
const fetchUser = (e: any) => {
e.preventDefault();
axios
.get(`https://java-react-app.herokuapp.com/api/account/${value}`)
.then((response) => {
const res = response.data;
setId(res.id);
setLogin(res.login);
setPassword(res.password);
})
.catch((error) => {
alert(error.name + ': ' + error.message);
});
};
Endpoint
@Stateless
@Path("/account")
public class AccountEndpoint {
@Inject
private AccountService accountService;
@GET
@Path("/{login}")
public Response getAccountByLogin(@PathParam("login") String login) {
Account account = accountService.getAccountByLogin(login);
if(account == null) return Response.status(404, "Account doesn't exist").build();
return Response.ok(account).build();
}
main
@ApplicationPath("/api")
public class HelloApplication extends Application {
}
result Basic rest also doesnt work
@Path("/hello-world")
public class HelloResource {
@GET
@Produces("text/plain")
public String hello() {
return "Hello, World!";
}
}
Everything works on localhost, but maybe is a specific way which i should fetching data from backend on heroku?
Also my pom
<?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>
<groupId>com.example</groupId>
<artifactId>jakarta-react-app</artifactId>
<version>1.0.0</version>
<name>jakarta-react-app</name>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.target>17</maven.compiler.target>
<maven.compiler.source>17</maven.compiler.source>
<junit.version>5.8.1</junit.version>
<jakarta.version>9.1.0</jakarta.version>
<failOnMissingWebXml>false</failOnMissingWebXml>
<frontend.directory>src/frontend</frontend.directory>
<frontend.directory.output>${frontend.directory}/build</frontend.directory.output>
</properties>
<dependencies>
<dependency>
<groupId>jakarta.enterprise</groupId>
<artifactId>jakarta.enterprise.cdi-api</artifactId>
<version>3.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jakarta.ws.rs</groupId>
<artifactId>jakarta.ws.rs-api</artifactId>
<version>3.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>5.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.0.0.Final</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.3.3</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
<dependency>
<groupId>jakarta.platform</groupId>
<artifactId>jakarta.jakartaee-web-api</artifactId>
<version>9.1.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.2</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<webResources>
<resource>
<directory>${frontend.directory.output}</directory>
</resource>
</webResources>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<filesets>
<fileset>
<directory>${frontend.directory.output}</directory>
<includes>
<include>**</include>
</includes>
<followSymlinks>false</followSymlinks>
</fileset>
</filesets>
</configuration>
</plugin>
<plugin>
<artifactId>exec-maven-plugin</artifactId>
<groupId>org.codehaus.mojo</groupId>
<version>3.0.0</version>
<executions>
<execution>
<id>Install dependencies</id>
<phase>generate-resources</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>npm</executable>
<arguments>
<argument>install</argument>
</arguments>
<workingDirectory>${frontend.directory}</workingDirectory>
</configuration>
</execution>
<execution>
<id>Build frontend</id>
<phase>generate-resources</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>npm</executable>
<arguments>
<argument>run</argument>
<argument>build</argument>
</arguments>
<workingDirectory>${frontend.directory}</workingDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
</project>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
