ResteasyClient client = (ResteasyClient)ClientBuilder.newClient();
ResteasyWebTarget target = client.target(url+ id);
Invocation.Builder request = target.request();
response = target.request().get();
System.out.println(response);
public class RestEasyClientDemo {
static String url ="http://localhost:8083/api";
public static void main(String args[]) {
person();
}
public static void person() {
ResteasyClient client = (ResteasyClient)ClientBuilder.newClient();
ResteasyWebTarget target = client.target(url+"/userdto/652");
Invocation.Builder request = target.request();
Response response = null;
response = request.get();
System.out.println(response.getLocation());
response.close();
client.close();
}
}
Ja genau, aber wie mache ich das?Du kannst halt irgendeinen Body (z. B. ein JSON-Objekt) per PUT- oder POST-Request (wenns ist auch PATCH) an den Server senden.
public class RestEasyClientDemo {
class Data implements Serializable{
Long id;
String name;
String ort;
String geb;
private void setID(Long id) {
this.id = id;
}
private Long getID() {
return this.id;
}
private void setName(String name) {
this.name = name;
}
private String getName() {
return this.name;
}
private void setOrt(String ort) {
this.ort = ort;
}
private String getOrt() {
return this.ort;
}
private void setGeb(String geb) {
this.geb = geb;
}
private String getGeb() {
return this.geb;
}
}
private static String url ="http://localhost:8083/api";
public static void main(String args[]) {
System.out.println("esay");
person();
}
@GET
@Produces(MediaType.APPLICATION_JSON)
public static void person() {
ResteasyClient client = (ResteasyClient)ClientBuilder.newClient();
List<Data> result = client.target(url).path("/userdto/652").request(MediaType.APPLICATION_JSON).get(new GenericType<List<Data>>() {
});
client.close();
System.out.println(result);
}
}
Wäre das nicht einfach der Aufruf:Jetzt muss ich ja "client.target(url).path("/userdto/652")..." so einstellen, dass es den User in der Klasse abbildet oder wie muss ich das verstehen?
Data result = client.target(url).path("/userdto/652").request(MediaType.APPLICATION_JSON).get(Data.class);
public class RestEasyClientDemo {
class Data implements Serializable{
Long id;
String name;
String ort;
String geb;
private void setID(Long id) {
this.id = id;
}
private Long getID() {
return this.id;
}
private void setName(String name) {
this.name = name;
}
private String getName() {
return this.name;
}
private void setOrt(String ort) {
this.ort = ort;
}
private String getOrt() {
return this.ort;
}
private void setGeb(String geb) {
this.geb = geb;
}
private String getGeb() {
return this.geb;
}
}
private static String url ="http://localhost:8083/api";
public static void main(String args[]) {
System.out.println("esay");
person();
}
public static void person() {
ResteasyClient client = (ResteasyClient)ClientBuilder.newClient();
Data result = client.target(url).path("/userdto/652").request(MediaType.APPLICATION_JSON).get(Data.class);
client.close();
System.out.println(result);
}
}
Exception in thread "main" javax.ws.rs.client.ResponseProcessingException: javax.ws.rs.ProcessingException: RESTEASY003145: Unable to find a MessageBodyReader of content-type application/json and type class resteasy.RestEasyClientDemo$Data
at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.extractResult(ClientInvocation.java:203)
at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.invoke(ClientInvocation.java:514)
at org.jboss.resteasy.client.jaxrs.internal.ClientInvocationBuilder.get(ClientInvocationBuilder.java:196)
at resteasy.RestEasyClientDemo.person(RestEasyClientDemo.java:70)
at resteasy.RestEasyClientDemo.main(RestEasyClientDemo.java:61)
Caused by: javax.ws.rs.ProcessingException: RESTEASY003145: Unable to find a MessageBodyReader of content-type application/json and type class resteasy.RestEasyClientDemo$Data
at org.jboss.resteasy.core.interception.jaxrs.ClientReaderInterceptorContext.throwReaderNotFound(ClientReaderInterceptorContext.java:47)
at org.jboss.resteasy.core.interception.jaxrs.AbstractReaderInterceptorContext.getReader(AbstractReaderInterceptorContext.java:133)
at org.jboss.resteasy.core.interception.jaxrs.AbstractReaderInterceptorContext.proceed(AbstractReaderInterceptorContext.java:75)
at org.jboss.resteasy.client.jaxrs.internal.ClientResponse.readFrom(ClientResponse.java:217)
at org.jboss.resteasy.specimpl.BuiltResponse.readEntity(BuiltResponse.java:90)
at org.jboss.resteasy.specimpl.AbstractBuiltResponse.readEntity(AbstractBuiltResponse.java:256)
at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.extractResult(ClientInvocation.java:167)
... 4 more
<?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>3.0.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.modelmapper/modelmapper -->
<dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>3.1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson-provider</artifactId>
<version>3.0.4.Final</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Also Deine POM zeigt ein Spring Boot Projekt. Daher die Frage. Wenn Du RESTeasy stand alone nutzt, dann würden wir da gerne die Abhängigkeiten sehen. Da wäre dann ja ein Spring Boot enthalten.Ich verwende normal RESTeasy.
<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>de.rest.client</groupId>
<artifactId>resteasy</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<resteasy.version>4.7.2.Final</resteasy.version>
</properties>
<dependencies>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<version>${resteasy.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
</dependencies>
</project>
Client client = ClientBuilder.newBuilder().build();
WebTarget target = client.target("http://localhost:8080/");
Response response = target.request().get();
Data value = response.readEntity(Data.class);
System.out.println(value);
response.close(); // You should close connections!
<!-- https://mvnrepository.com/artifact/org.jboss.resteasy/resteasy-client -->
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<version>6.2.2.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/jakarta.activation/jakarta.activation-api -->
<dependency>
<groupId>jakarta.activation</groupId>
<artifactId>jakarta.activation-api</artifactId>
<version>2.1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jboss.resteasy/resteasy-jackson2-provider -->
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson2-provider</artifactId>
<version>6.2.2.Final</version>
</dependency>
Data(name=Otto, info=Mag nicht mehr!)
@GetMapping("/")
public Data getData() {
return new Data("Otto", "Mag nicht mehr!");
}
public class Data implements Serializable{
private Long id;
private String name;
private String ort;
private String geb;
public Data() {
super();
}
public void setName(String name) {
this.name = name;
}
public void setID(Long id) {
this.id = id;
}
public void setOrt(String ort) {
this.ort = ort;
}
public void setGeb(String geb) {
this.geb = geb;
}
public String getName() {
return this.name;
}
public String getOrt() {
return this.ort;
}
Data[] value = response.readEntity(new GenericType<Data[]>() {});
//ein Objekt ausgeben
public void einen() {
Client client = ClientBuilder.newBuilder().build();
WebTarget target = client.target(url).path("/userdto/702");
Response response = target.request().get();
Data value = response.readEntity(Data.class);
System.out.println(value.getName());
client.close();
response.close();
}
//alle Objekte ausgeben
public void alle() {
Client client = ClientBuilder.newBuilder().build();
WebTarget target = client.target(url).path("/all");
Response response = target.request().get();
List<Data> value = (List<Data>) response.readEntity(new GenericType<List<Data>>() {});
for (Data s : value) {
System.out.print(" " + s.getName());
System.out.print(" " + s.getOrt());
System.out.print(" " + s.getGeb());
System.out.print(" " + s.getID());
}
client.close();
response.close();
}
//Objekt hinzufuegen
public void adden() {
Client client = ClientBuilder.newBuilder().build();
WebTarget target = client.target(url).path("/jsonadd");
Data data = new Data();
data.setGeb("1.1.24");
data.setName("resteasy");
data.setOrt("fuchshausen");
Response response = target.request().post(Entity.json(data));
System.out.println(response.getStatusInfo());
}
//aendern eines Objektes
public void putten() {
Client client = ClientBuilder.newBuilder().build();
WebTarget target = client.target(url).path("/jsonput/702");
Data data = new Data();
data.setName("mustermann");
data.setGeb("1.1.22");
data.setOrt("Musterstadt");
Response response = target.request().put(Entity.json(data));
System.out.println(response.getStatusInfo());
client.close();
response.close();
}
//loeschen
//hier gebe ich die ID per Parameter an
public void delete() {
Client client = ClientBuilder.newBuilder().build();
WebTarget target = client.target(url).path("/del").queryParam("id", "705");
Response response = target.request().delete();
System.out.println(response.getStatusInfo());
client.close();
}