如何结合 HttpServletRequest + @RestController

why*_*ent 2 java spring

我已经使用 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
    }

}
Run Code Online (Sandbox Code Playgroud)

我还使用谷歌库,它要求我编写一个看起来像这样的类(取自谷歌示例):

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();
  }
}
Run Code Online (Sandbox Code Playgroud)

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

编辑:

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

And*_*niy 5

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

public JsonCompany get(HttpServletRequest req, @RequestParam("name") String name)  {           
       //omitted
}
Run Code Online (Sandbox Code Playgroud)

它会自动填充,您可以在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
   }
}
Run Code Online (Sandbox Code Playgroud)

如果这不是您正在寻找的,那么我不明白您需要什么。