官术网_书友最值得收藏!

  • 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
}
We set the JAX-RS @ApplicationPath to  /api to ensure that our endpoints are deployed under the  /api subcontext.
主站蜘蛛池模板: 环江| 出国| 张家界市| 邻水| 阿城市| 桃园市| 莱阳市| 南召县| 深圳市| 年辖:市辖区| 通海县| 达拉特旗| 宜阳县| 罗城| 离岛区| 元氏县| 中西区| 平武县| 湘乡市| 舒城县| 婺源县| 章丘市| 高清| 抚州市| 乡宁县| 佛教| 惠州市| 肇东市| 河东区| 左贡县| 崇义县| 浙江省| 丘北县| 团风县| 云霄县| 贵南县| 自贡市| 闽侯县| 福安市| 苍溪县| 甘孜|