- Java EE 8 High Performance
- Romain Manni Bucau
- 260字
- 2021-06-30 19:14:24
The JAX-RS layer
If we step back one second and think about which stopover the application will execute, we can identify a few of them:
- HTTP communication handling
- Payload (un)marshalling
- Routing
- Service invocation
Because of the separation of concern principles, or simply for technical constraints between layers, it is very common to use a Data Transfer Object between the JAX-RS/front layer and the CDI/business layer. Of course, this statement can be applied to the business sub-layers as well, but in the case of this book, we will just do it in the JAX-RS layer. To make it obvious in the book, we will prefix the JAX-RS model with Json. Check out the following code snippet:
@JsonbPropertyOrder({"id", "name", "customerCount"})
public class JsonQuote {
private long id;
private String name;
private double value;
@JsonbProperty("customer_count")
private long customerCount;
// getters/setters
}
In this context, the front layer role is to delegate most of the logic to the service layer and convert the business model to the front model (it can almost be seen as a Java to JavaScript conversion for a lot of modern applications):
@Path("quote")
@RequestScoped
public class QuoteResource {
@Inject
private QuoteService quoteService;
@GET
@Path("{id}")
public JsonQuote findById(@PathParam("id") final long id) {
return quoteService.findById(id) // delegation to the business
layer
.map(quote -> { // the model conversion
final JsonQuote json = new JsonQuote();
json.setId(quote.getId());
json.setName(quote.getName());
json.setValue(quote.getValue());
json.setCustomerCount(ofNullable(quote.getCustomers())
.map(Collection::size).orElse(0));
return json;
})
.orElseThrow(() -> new
WebApplicationException(Response.Status.NO_CONTENT));
}
// other methods
}
- 從零開(kāi)始寫(xiě)Linux內(nèi)核:一書(shū)學(xué)透核心原理與實(shí)現(xiàn)
- Linux內(nèi)核完全注釋?zhuān)?0周年版·第2版)
- Kali Linux滲透測(cè)試全流程詳解
- 8051軟核處理器設(shè)計(jì)實(shí)戰(zhàn)
- Android物聯(lián)網(wǎng)開(kāi)發(fā)細(xì)致入門(mén)與最佳實(shí)踐
- Kali Linux 2018:Windows Penetration Testing
- INSTANT Galleria Howto
- OpenHarmony開(kāi)發(fā)與實(shí)踐:基于紅莓RK2206開(kāi)發(fā)板
- Angular權(quán)威教程
- 操作系統(tǒng)之哲學(xué)原理第2版
- Windows 8玩全不求人
- Python機(jī)器學(xué)習(xí)系統(tǒng)構(gòu)建(原書(shū)第3版)
- 從零開(kāi)始學(xué)Windows 7
- Embedded Systems Architecture
- OpenStack Trove Essentials