org.springframework.web.servlet.function.ServerResponse Java Examples

The following examples show how to use org.springframework.web.servlet.function.ServerResponse. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SpringBootMvcFnApplication.java    From tutorials with MIT License 6 votes vote down vote up
@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()));
}
 
Example #2
Source File: HandlerFunctionAdapter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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));
}
 
Example #3
Source File: HelloApplication.java    From springdoc-openapi with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example #4
Source File: ProductController.java    From tutorials with MIT License 5 votes vote down vote up
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();
}
 
Example #5
Source File: ProductController.java    From tutorials with MIT License 5 votes vote down vote up
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();
}
 
Example #6
Source File: HelloApplication.java    From springdoc-openapi with Apache License 2.0 4 votes vote down vote up
ServerResponse handleGetAllPeople(ServerRequest serverRequest) {
	return ok().body(personService.all());
}
 
Example #7
Source File: HelloApplication.java    From springdoc-openapi with Apache License 2.0 4 votes vote down vote up
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);
}
 
Example #8
Source File: HelloApplication.java    From springdoc-openapi with Apache License 2.0 4 votes vote down vote up
ServerResponse handleGetPersonById(ServerRequest r) {
	return ok().body(personService.byId(Long.parseLong(r.pathVariable("id"))));
}
 
Example #9
Source File: SampleHandler.java    From spring-fu with Apache License 2.0 4 votes vote down vote up
public ServerResponse hello(ServerRequest request) {
	return ok().body(sampleService.generateMessage());
}
 
Example #10
Source File: SampleHandler.java    From spring-fu with Apache License 2.0 4 votes vote down vote up
public ServerResponse json(ServerRequest request) {
	return ok().body(new Sample(sampleService.generateMessage()));
}
 
Example #11
Source File: ProductController.java    From tutorials with MIT License 4 votes vote down vote up
public RouterFunction<ServerResponse> productListing(ProductService ps) {
    return route().GET("/product", req -> ok().body(ps.findAll()))
        .build();
}
 
Example #12
Source File: ProductController.java    From tutorials with MIT License 4 votes vote down vote up
public RouterFunction<ServerResponse> remainingProductRoutes(ProductService ps) {
    return route().add(productSearch(ps))
        .add(adminFunctions(ps))
        .build();
}
 
Example #13
Source File: SpringBootMvcFnApplication.java    From tutorials with MIT License 4 votes vote down vote up
@Bean
RouterFunction<ServerResponse> productListing(ProductController pc, ProductService ps) {
    return pc.productListing(ps);
}