Skip to content

Commit fa6e3d0

Browse files
committed
Fix the problem of using @RestController annotation to return data results
1 parent 6f98edf commit fa6e3d0

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

netty/src/main/java/io/edurt/gcm/netty/annotation/RestController.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
@Target(ElementType.TYPE)
2222
@Retention(RetentionPolicy.RUNTIME)
23+
@ResponseBody
2324
public @interface RestController
2425
{
2526
String value() default "";

netty/src/main/java/io/edurt/gcm/netty/dispatcher/RequestDispatcher.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import io.edurt.gcm.netty.annotation.RequestBody;
99
import io.edurt.gcm.netty.annotation.RequestParam;
1010
import io.edurt.gcm.netty.annotation.ResponseBody;
11+
import io.edurt.gcm.netty.annotation.RestController;
1112
import io.edurt.gcm.netty.configuration.NettyConfiguration;
1213
import io.edurt.gcm.netty.configuration.NettyConfigurationDefault;
1314
import io.edurt.gcm.netty.exception.NettyException;
@@ -97,7 +98,8 @@ public void triggerAction(FullHttpRequest httpRequest, FullHttpResponse httpResp
9798
LOGGER.debug("Parsing method parameters, used to inject the corresponding entity!");
9899
ArrayList<Class> classList = new ArrayList<>();
99100
ArrayList<Object> objectList = new ArrayList<>();
100-
Object ctrlObject = injector.getInstance(Class.forName(ctrlClass));
101+
Class<?> clazz = Class.forName(ctrlClass);
102+
Object ctrlObject = injector.getInstance(clazz);
101103
LOGGER.debug("Current execute controller {}", ctrlObject);
102104
Map<String, String> requestParams = this.getRequestParams(httpRequest);
103105
Method[] methods = ctrlObject.getClass().getMethods();
@@ -155,7 +157,8 @@ else if (parameter.getAnnotation(RequestBody.class) != null) {
155157
Object[] objects = objectList.toArray();
156158
Method method = ctrlObject.getClass().getMethod(methodName, classes);
157159
String content = null;
158-
if (method.isAnnotationPresent(ResponseBody.class)) {
160+
// Fix the problem of using @RestController annotation to return data results
161+
if (method.isAnnotationPresent(ResponseBody.class) || clazz.isAnnotationPresent(RestController.class)) {
159162
Gson gson = new Gson();
160163
try {
161164
content = gson.toJson(method.invoke(ctrlObject, objects));
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
package io.edurt.gcm.netty.controller;
15+
16+
import io.edurt.gcm.netty.annotation.GetMapping;
17+
import io.edurt.gcm.netty.annotation.RequestParam;
18+
import io.edurt.gcm.netty.annotation.RestController;
19+
20+
@RestController
21+
public class TestImplicitResponseController
22+
{
23+
@GetMapping(value = {"/api/test/implicit/get"})
24+
public String getMapping(@RequestParam(value = "value") String value)
25+
{
26+
return String.format("This is @GetMapping test case, value %s", value);
27+
}
28+
}

0 commit comments

Comments
 (0)