<dictionary>
<word>
<source>house</source>
<target>casă</target>
</word>
</dictionary>
package service;
import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.ClientBuilder;
import jakarta.ws.rs.core.Response;
public class TranslationClient {
public static void main(String[] args) {
Client client = ClientBuilder.newClient();
String word = "house";
String sourceLang = "english";
String targetLang = "romanian";
Response response = client.target("http://localhost:8080/JerseyRESETService/rest/translation")
.queryParam("word", word)
.queryParam("sourceLang", sourceLang)
.queryParam("targetLang", targetLang)
.request().get();
if (response.getStatus() == 200) {
String translation = response.readEntity(String.class);
System.out.println("Перевод слова '" + word + "' это: " + translation);
} else {
String errorMessage = response.readEntity(String.class);
System.out.println("Error: " + errorMessage);
}
client.close();
}
}
package service;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.core.MediaType;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
@Path("/translation")
public class TranslationService {
private static final String XML_FILE_PATH = "IdeaProjects\\WebApp\\src\\main\\java\\service\\dictionary.xml";
@GET
@Produces(MediaType.TEXT_PLAIN)
public String translateWord(@QueryParam("word") String word,
@QueryParam("sourceLang") String sourceLang,
@QueryParam("targetLang") String targetLang) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(XML_FILE_PATH);
NodeList wordNodes = doc.getElementsByTagName("word");
for (int i = 0; i < wordNodes.getLength(); i++) {
Element wordElement = (Element) wordNodes.item(i);
String sourceWord = wordElement.getElementsByTagName("source").item(0).getTextContent();
String targetWord = wordElement.getElementsByTagName("target").item(0).getTextContent();
if (sourceWord.equalsIgnoreCase(word) && sourceLang.equalsIgnoreCase("english") &&
targetLang.equalsIgnoreCase("romanian")) {
return targetWord;
}
}
return "Слово не найдено в базе данных.";
} catch (Exception e) {
e.printStackTrace();
return "При обработке запроса произошла ошибка.";
}
}
}
web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>RESTWebService</display-name>
<servlet>
<servlet-name>JerseyRESETService</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>service</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>JerseyRESETService</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
index.jsp
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>
pom.xml
<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 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>service</groupId>
<artifactId>WebApp</artifactId>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>20</maven.compiler.source>
<maven.compiler.target>20</maven.compiler.target>
</properties>
<version>1.0-SNAPSHOT</version>
<name>WebApp Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>3.1.3</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>3.1.3</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<version>3.1.3</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>3.1.3</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<version>6.2.5.Final</version>
</dependency>
</dependencies>
<build>
<finalName>RESTWebService</finalName>
<plugins>
<plugin>
<groupId>org.apache.tomee.maven</groupId>
<artifactId>tomee-maven-plugin</artifactId>
<version>9.1.0</version>
</plugin>
</plugins>
</build>
</project>
При запуск кода мне выдает такую ошибку. Как её исправить ?
Указывает ошибку на get();
Exception in thread "main" jakarta.ws.rs.ProcessingException: java.net.ConnectException: Connection refused: connect
at org.glassfish.jersey.client.internal.HttpUrlConnector.apply(HttpUrlConnector.java:275)
at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:300)
at org.glassfish.jersey.client.JerseyInvocation.lambda$invoke$0(JerseyInvocation.java:662)
at org.glassfish.jersey.client.JerseyInvocation.call(JerseyInvocation.java:697)
at org.glassfish.jersey.client.JerseyInvocation.lambda$runInScope$3(JerseyInvocation.java:691)
at org.glassfish.jersey.internal.Errors.process(Errors.java:292)
at org.glassfish.jersey.internal.Errors.process(Errors.java:274)
at org.glassfish.jersey.internal.Errors.process(Errors.java:205)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:390)
at org.glassfish.jersey.client.JerseyInvocation.runInScope(JerseyInvocation.java:691)
at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:661)
at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:413)
at org.glassfish.jersey.client.JerseyInvocation$Builder.get(JerseyInvocation.java:313)
at service.TranslationClient.main(TranslationClient.java:20)
Caused by: java.net.ConnectException: Connection refused: connect
at java.base/sun.nio.ch.Net.connect0(Native Method)
at java.base/sun.nio.ch.Net.connect(Net.java:580)
at java.base/sun.nio.ch.Net.connect(Net.java:569)
at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:581)
at java.base/java.net.Socket.connect(Socket.java:666)
at java.base/sun.net.NetworkClient.doConnect(NetworkClient.java:178)
at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:532)
at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:637)
at java.base/sun.net.www.http.HttpClient.(HttpClient.java:280)
at java.base/sun.net.www.http.HttpClient.New(HttpClient.java:385)
at java.base/sun.net.www.http.HttpClient.New(HttpClient.java:407)
at java.base/sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1308)
at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1241)
at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1127)
at java.base/sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:1056)
at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1657)
at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1581)
at java.base/java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:529)
at org.glassfish.jersey.client.internal.HttpUrlConnector._apply(HttpUrlConnector.java:415)
at org.glassfish.jersey.client.internal.HttpUrlConnector.apply(HttpUrlConnector.java:273)
... 13 more
Process finished with exit code 1