Dropwizard + Raml -> 空资源
Dropwizard + Raml -> empty resource
当我启动服务器时,有一条警告说我的资源是空的(结果是 404):
WARN org.glassfish.jersey.internal.Errors - The following warnings have been detected: WARNING: A resource, Resource{"helloz", 0 child resources, 0 resource methods, 0 sub-resource locator, 0 method handler classes, 0 method handler instances}, with path "helloz" is empty. It has no resource (or sub resource) methods neither sub resource locators defined.
拉姆尔:
#%RAML 0.8
---
title: API
version: 0.1
mediaType: application/json
baseUri: "{apiRoot}/api"
baseUriParameters:
apiRoot:
description: Set the root of the service here
example: http://localhost:9090
schemas:
- common: !include common-schema.json
- hello-schema: |
{ "$schema": "http://json-schama.org/draft-03/schema",
"type": "object",
"description": "A Hello World Schema",
"properties": {
"times": { "type": "number"}
}
}
- meta-error: |
{ "$schema": "http://json-schama.org/draft-03/schema",
"type": "object",
"description": "A generic error response for the API",
"properties": {
"message": { "type": "string" },
"details": { "type": "array",
"items": { "type": "string" }
}
}
}
traits:
- secured:
usage: Apply this to any method that needs to be secured
description: Some requests require authentication.
queryParameters:
access_token:
description: Access Token
type: string
example: ACCESS_TOKEN
# required: true
/helloz:
is: [ secured ]
post:
description: Just say hello
body:
application/json:
schema: hello-schema
example: !include examples/empty.json
responses:
200:
body:
application/json:
schema: hello-schema
400:
description: A malformed input message
body:
application/json:
schema: meta-error
实施:
public class SayHello implements Helloz {
@Override
public PostHellozResponse postHelloz(@QueryParam("access_token") String accessToken, HelloSchema entity) throws Exception {
System.out.println("Hello World!");
return PostHellozResponse.withJsonOK(null);
}
}
在 Application#运行 中,我只执行以下操作:environment.jersey().register(SayHello.class);
好的,这并不明显 - 您需要删除实施中的注释 class:
错误
public class SayHello implements Helloz {
@Override
public PostHellozResponse postHelloz(@QueryParam("access_token") String accessToken, HelloSchema entity) throws Exception {
System.out.println("Hello World!");
return PostHellozResponse.withJsonOK(null);
}
}
正确:
public class SayHello implements Helloz {
@Override
public PostHellozResponse postHelloz(String accessToken, HelloSchema entity) throws Exception {
System.out.println("Hello World!");
return PostHellozResponse.withJsonOK(null);
}
}
当我启动服务器时,有一条警告说我的资源是空的(结果是 404):
WARN org.glassfish.jersey.internal.Errors - The following warnings have been detected: WARNING: A resource, Resource{"helloz", 0 child resources, 0 resource methods, 0 sub-resource locator, 0 method handler classes, 0 method handler instances}, with path "helloz" is empty. It has no resource (or sub resource) methods neither sub resource locators defined.
拉姆尔:
#%RAML 0.8
---
title: API
version: 0.1
mediaType: application/json
baseUri: "{apiRoot}/api"
baseUriParameters:
apiRoot:
description: Set the root of the service here
example: http://localhost:9090
schemas:
- common: !include common-schema.json
- hello-schema: |
{ "$schema": "http://json-schama.org/draft-03/schema",
"type": "object",
"description": "A Hello World Schema",
"properties": {
"times": { "type": "number"}
}
}
- meta-error: |
{ "$schema": "http://json-schama.org/draft-03/schema",
"type": "object",
"description": "A generic error response for the API",
"properties": {
"message": { "type": "string" },
"details": { "type": "array",
"items": { "type": "string" }
}
}
}
traits:
- secured:
usage: Apply this to any method that needs to be secured
description: Some requests require authentication.
queryParameters:
access_token:
description: Access Token
type: string
example: ACCESS_TOKEN
# required: true
/helloz:
is: [ secured ]
post:
description: Just say hello
body:
application/json:
schema: hello-schema
example: !include examples/empty.json
responses:
200:
body:
application/json:
schema: hello-schema
400:
description: A malformed input message
body:
application/json:
schema: meta-error
实施:
public class SayHello implements Helloz {
@Override
public PostHellozResponse postHelloz(@QueryParam("access_token") String accessToken, HelloSchema entity) throws Exception {
System.out.println("Hello World!");
return PostHellozResponse.withJsonOK(null);
}
}
在 Application#运行 中,我只执行以下操作:environment.jersey().register(SayHello.class);
好的,这并不明显 - 您需要删除实施中的注释 class:
错误
public class SayHello implements Helloz {
@Override
public PostHellozResponse postHelloz(@QueryParam("access_token") String accessToken, HelloSchema entity) throws Exception {
System.out.println("Hello World!");
return PostHellozResponse.withJsonOK(null);
}
}
正确:
public class SayHello implements Helloz {
@Override
public PostHellozResponse postHelloz(String accessToken, HelloSchema entity) throws Exception {
System.out.println("Hello World!");
return PostHellozResponse.withJsonOK(null);
}
}