最終更新日:2020/02/13 原本2018/06/20

Rest client using fxml (javaFX)

 
Rancher
Posts: 1170
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys (and girls)

I did some javaFX stuff, but always maded standalone applications.
It's my understanding that I could use javaFX as frontend for a restclient.
Before I only used jsp's, but I like javaFX better, so I really want to make the change, but I have no idea as how to start on the client.
I guess the serverside would be as any other restfull application?!?
Anyone who can point me in the direction of some easy to follow tutorials or can give me some pointers to watch out for?
I already know I should use a background thread to request the resources, but that's about it...
An example project on git would also be nice, from there I will be able to figure it out and if not there's always coderanch :p

Thanks....
 
Saloon Keeper
Posts: 11109
244
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JAX-RS includes a client API that you can use to access REST services.

I suggest that you create a module that contains interfaces for the services that you want to use, with data transfer objects that can be serialized through JAX-B. On the server-side, implement the interfaces with REST controllers, and on the client side, implement the interfaces using an instance of WebTarget that represents the base address of your API.

Services module:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.example.hr.service;
 
import java.util.*;
 
import com.example.hr.service.data.*;
 
public interface EmployeeService {
 
  public Map<String, EmployeeData> indexEmployees();
 
  public EmployeeData createEmployee(EmployeeData data);
 
  public EmployeeData getEmployee(String id);
 
  public EmployeeData updateEmployee(String id, EmployeeData data);
 
  public EmployeeData deleteEmployee(String id);
 
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package com.example.hr.service.data;
 
import java.util.*;
 
import javax.xml.bind.annotation.*;
 
import com.example.person.data.*;
 
import static java.util.Collections.*;
 
@XmlRootElement(name = "employee", namespace = "http://example.com/hr/v1")
@XmlAccessorType(XmlAccessType.FIELD)
public final class EmployeeData {
 
  @XmlAttribute
  private String id;
 
  @XmlElement(namespace = "http://example.com/person/v3")
  private PersonalNameData name;
 
  @XmlElement
  private SalaryData salary;
 
  @XmlElementWrapper
  @XmlElement(name = "role")
  private Collection<RoleData> roles;
 
  public EmployeeData() {}
 
  public EmployeeData(String id, PersonalNameData name, SalaryData salary, Collection<? extends RoleData> roles) {
    setId(id);
    setName(name);
    setSalary(salary);
    setRoles(roles);
  }
 
  public Optional<String> id() {
    return Optional.ofNullable(id);
  }
 
  public void setId(String id) {
    if (id == null)
      this.id = id;
      return;
    }
 
    String trimmedId = id.trim();
 
    if (trimmedId.isEmpty() || !trimmedId.equals(id))
      throw new IllegalArgumentException("The ID must be a trimmed non-empty string, or null.");
 
    this.id = trimmedId;
  }
 
  public Optional<PersonalNameData> name() {
    return Optional.ofNullable(name);
  }
 
  public void setName(PersonalNameData name) {
    this.name = name;
  }
 
  public Optional<SalaryData> salary() {
    return Optional.ofNullable(salary);
  }
 
  public void setSalary(SalaryData salary) {
    this.salary = salary;
  }
 
  public Collection<RoleData> roles() {
    return unmodifiableList(roles);
  }
 
  public void setRoles(Collection<? extends RoleData> roles) {
    Collection<RoleData> copy = new ArrayList<>(roles);
    if (copy.contains(null))
      throw new IllegalArgumentException("The roles collection must not contain null.");
 
    this.roles = copy;
  }
}

Server module:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.example.hr.server.controller;
 
import java.util.*;
 
import javax.inject.*;
 
import javax.ws.rs.*;
import javax.ws.rs.core.*;
 
import com.example.hr.server.*;
import com.example.hr.server.repository.*;
import com.example.hr.service.*;
import com.example.hr.service.data.*;
 
@Path("employees");
@Consumes({MediaType.APPLICATION_XML})
@Produces({MediaType.APPLICATION_XML})
public final class EmployeeController implements EmployeeService {
 
  private final EmployeeRepository repository;
 
  @Inject
  public EmployeeController(EmployeeRepository repository) {
    if (repository == null)
      throw new IllegalArgumentException("The repository must not be null.");
 
    this.repository = repository;
  }
 
  ...
 
  @PUT
  @Path("{id}")
  @Override
  public EmployeeData updateEmployee(@PathParam("id") String id, EmployeeData data) {
    try {
      Employee employee = repository.findById(id).orElseThrow(NotFoundException::new);
      employee.updateFromData(data);
      repository.update(employee);
      return employee.getData();
    }
 
    catch (RepositoryException ex) {
      throw new InternalServerErrorException(ex);
    }
  }
 
  ...
 
}

Client module:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.example.hr.client.service;
 
import javax.ws.rs.client.*;
 
import com.example.hr.service.*;
import com.example.hr.service.data.*;
 
public final class EmployeeServiceProxy implements EmployeeService {
 
  private final Client restClient;
 
  private final URI apiBaseUri;
 
  public EmployeeServiceProxy(Client restClient, URI apiBaseUri) {
    if (restClient == null)
      throw new IllegalArgumentException("The REST client must not be null.");
 
    if (apiBaseUri == null)
      throw new IllegalArgumentException("The API's base URI must not be null.");
 
    this.restClient = restClient;
    this.apiBaseUri = apiBaseUri;
  }
 
  private WebTarget target(String path) {
    return restClient.target(UriBuilder.fromUri(apiBaseUri).path(path));
  }
 
  ...
 
  @Override
  public EmployeeData updateEmployee(String id, EmployeeData data) {
    return target("employees/{id}").resolveTemplate("id", id).request().buildPut(Entity.xml(data)).invoke(EmployeeData.class);
  }
 
  ...
 
}


Now, after you've constructed an instance of EmployeeServiceProxy, you can call its methods and it will be as if you're directly calling the controllers on the server-side.

I've written this code without any tools on hand, so I don't know if it is correct. Use it as an inspiration.
 
Daniel Demesmaecker
Rancher
Posts: 1170
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a bunch Stephan, that at least should get me started. Cheers
 
Hey, check out my mega multi devastator cannon. It's wicked. It makes this tiny ad look weak:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java