用户工具

站点工具


java:light4j

docs

startup

hello world

 //HelloWorldHandler.java
 public class HelloWorldHandler implements LightHttpHandler {
    @Override
    public void handleRequest(HttpServerExchange exchange) throws Exception {
        System.out.println(exchange.getQueryParameters().get("name"));
        String name = exchange.getQueryParameters().get("name").peek();
        System.out.println(exchange.getQueryString());
        exchange.getResponseHeaders().add(new HttpString("Content-Type"), "application/text");
        exchange.getResponseSender().send("hello " + name + "! welcome to Light 4J!!!");
    }
 }
 # handler.yml
paths:
  - path: 'v1/pets/helloworld' 
    method: 'GET'
    exec:
        - default
        - com.networknt.petstore.handler.HelloWorldHandler 
handlers:
  # Business Handlers
   - com.networknt.petstore.handler.HelloWorldHandler

https://127.0.0.1:8443/v1/pets/helloworld?name=nagappanan

tutorials

CORS

Cross-Origin Resource Sharing,默认off,方便接口被nodejs等实现的单页应用等访问,

        <dependency>
            <groupId>com.networknt</groupId>
            <artifactId>cors</artifactId>
            <version>${version.light-4j}</version>
        </dependency>
# service.yml
  #Cors handler to handler post/put pre-flight
  - com.networknt.cors.CorsHttpHandler
  # Parsing OpenAPI 3.0 specification based on request uri and method.
  - com.networknt.openapi.OpenApiHandler
java/light4j.txt · 最后更改: 2019/04/23 23:09 由 admin