gpt4 book ai didi

java - 如何结合 HttpServletRequest + @RestController

转载 作者:行者123 更新时间:2023-11-30 02:41:22 26 4
gpt4 key购买 nike

我已经使用 Java/Spring MVC 开发了当前的 Web 应用程序。我通过在类上使用 @RestController 注释公开了我的 REST 服务,如下所示:

@RestController
@RequestMapping("/company")
public class ConcreteCompanyController implements CompanyController {

@RequestMapping(method = RequestMethod.GET)
@ResponseStatus(value = HttpStatus.OK)
@Override
public JsonCompany get(@RequestParam("name") String name) {
//omitted
}

}

我还使用 Google 库,它要求我编写一个类似这样的类(取自 google 示例):

public class CalendarAppEngineSample extends AbstractAppEngineAuthorizationCodeServlet {

static final String APP_NAME = "Google Calendar Data API Sample Web Client";

static final String GWT_MODULE_NAME = "calendar";

private static final long serialVersionUID = 1L;

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException {
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
PrintWriter writer = response.getWriter();
writer.println("<!doctype html><html><head>");
writer.println("<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\">");
writer.println("<title>" + APP_NAME + "</title>");
writer.println(
"<link type=\"text/css\" rel=\"stylesheet\" href=\"" + GWT_MODULE_NAME + ".css\">");
writer.println("<script type=\"text/javascript\" language=\"javascript\" " + "src=\""
+ GWT_MODULE_NAME + "/" + GWT_MODULE_NAME + ".nocache.js\"></script>");
writer.println("</head><body>");
UserService userService = UserServiceFactory.getUserService();
writer.println("<div class=\"header\"><b>" + request.getUserPrincipal().getName() + "</b> | "
+ "<a href=\"" + userService.createLogoutURL(request.getRequestURL().toString())
+ "\">Log out</a> | "
+ "<a href=\"http://code.google.com/p/google-api-java-client/source/browse"
+ "/calendar-appengine-sample?repo=samples\">See source code for "
+ "this sample</a></div>");
writer.println("<div id=\"main\"/>");
writer.println("</body></html>");
}

@Override
protected String getRedirectUri(HttpServletRequest req) throws ServletException, IOException {
return Utils.getRedirectUri(req);
}

@Override
protected AuthorizationCodeFlow initializeFlow() throws IOException {
return Utils.newFlow();
}
}

我不知道如何将它们结合起来,以便两者都可以作为休息服务使用。我想我可能需要在 web.xml 中做一些事情,但是什么呢?

编辑:

只是为了澄清。我的项目中有几个 RestController,我不想更改它们。我希望能够添加像 CalendarAppEngineSample 这样的文件,并确保 doGet 方法作为休息服务公开。

最佳答案

您只需将其包含在方法参数列表中即可:

public JsonCompany get(HttpServletRequest req, @RequestParam("name") String name)  {           
//omitted
}

它会自动填充,您可以在 get 方法中使用它。

UPD根据您的上一条评论,如果您需要扩展类AbstractAppEngineAuthorizationCodeServlet并保留其签名,请执行以下操作:

@RestController
@RequestMapping("/company")
public class ConcreteCompanyController extends AbstractAppEngineAuthorizationCodeServlet {

@RequestMapping(method = RequestMethod.GET)
@ResponseStatus(value = HttpStatus.OK)
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
String name = request.getParameter("name");
// omitted code
}
}

如果这不是您想要的,那么我不明白您需要什么。

关于java - 如何结合 HttpServletRequest + @RestController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41590632/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com