It's good to be able to use someting, it's better to understand how it works.
www.goanation.net
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.*;@XmlAccessorType(XmlAccessType.FIELD)public final class EmployeeData { @XmlAttribute private String id; 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; }} |
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); } } ...} |
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); } ...} |
It's good to be able to use someting, it's better to understand how it works.
www.goanation.net
|
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
|