Java源码示例:org.springframework.web.servlet.function.ServerResponse
示例1
@Bean
RouterFunction<ServerResponse> allApplicationRoutes(ProductController pc, ProductService ps) {
return route().add(pc.remainingProductRoutes(ps))
.before(req -> {
LOG.info("Found a route which matches " + req.uri()
.getPath());
return req;
})
.after((req, res) -> {
if (res.statusCode() == HttpStatus.OK) {
LOG.info("Finished processing request " + req.uri()
.getPath());
} else {
LOG.info("There was an error while processing request" + req.uri());
}
return res;
})
.onError(Throwable.class, (e, res) -> {
LOG.error("Fatal exception has occurred", e);
return status(HttpStatus.INTERNAL_SERVER_ERROR).build();
})
.build()
.and(route(RequestPredicates.all(), req -> notFound().build()));
}
示例2
@Nullable
@Override
public ModelAndView handle(HttpServletRequest servletRequest,
HttpServletResponse servletResponse,
Object handler) throws Exception {
HandlerFunction<?> handlerFunction = (HandlerFunction<?>) handler;
ServerRequest serverRequest = getServerRequest(servletRequest);
ServerResponse serverResponse = handlerFunction.handle(serverRequest);
return serverResponse.writeTo(servletRequest, servletResponse,
new ServerRequestContext(serverRequest));
}
示例3
@Bean
@RouterOperations({ @RouterOperation(path = "/people", method = RequestMethod.GET, beanClass = PersonService.class, beanMethod = "all"),
@RouterOperation(path = "/people/{id}", beanClass = PersonService.class, beanMethod = "byId"),
@RouterOperation(path = "/people", method = RequestMethod.POST, beanClass = PersonService.class, beanMethod = "save") })
RouterFunction<ServerResponse> routes(PersonHandler ph) {
String root = "";
return route()
.GET(root + "/people", ph::handleGetAllPeople)
.GET(root + "/people/{id}", ph::handleGetPersonById)
.POST(root + "/people", ph::handlePostPerson)
.filter((serverRequest, handlerFunction) -> {
return handlerFunction.handle(serverRequest);
})
.build();
}
示例4
public RouterFunction<ServerResponse> productSearch(ProductService ps) {
return route().nest(RequestPredicates.path("/product"), builder -> {
builder.GET("/name/{name}", req -> ok().body(ps.findByName(req.pathVariable("name"))))
.GET("/id/{id}", req -> ok().body(ps.findById(Integer.parseInt(req.pathVariable("id")))));
})
.onError(ProductService.ItemNotFoundException.class, (e, req) -> EntityResponse.fromObject(new Error(e.getMessage()))
.status(HttpStatus.NOT_FOUND)
.build())
.build();
}
示例5
public RouterFunction<ServerResponse> adminFunctions(ProductService ps) {
return route().POST("/product", req -> ok().body(ps.save(req.body(Product.class))))
.filter((req, next) -> authenticate(req) ? next.handle(req) : status(HttpStatus.UNAUTHORIZED).build())
.onError(IllegalArgumentException.class, (e, req) -> EntityResponse.fromObject(new Error(e.getMessage()))
.status(HttpStatus.BAD_REQUEST)
.build())
.build();
}
示例6
ServerResponse handleGetAllPeople(ServerRequest serverRequest) {
return ok().body(personService.all());
}
示例7
ServerResponse handlePostPerson(ServerRequest r) throws ServletException, IOException {
Person result = personService.save(new Person(null, r.body(Person.class).getName()));
URI uri = URI.create("/people/" + result.getId());
return ServerResponse.created(uri).body(result);
}
示例8
ServerResponse handleGetPersonById(ServerRequest r) {
return ok().body(personService.byId(Long.parseLong(r.pathVariable("id"))));
}
示例9
public ServerResponse hello(ServerRequest request) {
return ok().body(sampleService.generateMessage());
}
示例10
public ServerResponse json(ServerRequest request) {
return ok().body(new Sample(sampleService.generateMessage()));
}
示例11
public RouterFunction<ServerResponse> productListing(ProductService ps) {
return route().GET("/product", req -> ok().body(ps.findAll()))
.build();
}
示例12
public RouterFunction<ServerResponse> remainingProductRoutes(ProductService ps) {
return route().add(productSearch(ps))
.add(adminFunctions(ps))
.build();
}
示例13
@Bean
RouterFunction<ServerResponse> productListing(ProductController pc, ProductService ps) {
return pc.productListing(ps);
}