- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经使用 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/
关于 SpringBoot 应用程序的小问题,以及如何获取用 @RestController 注释的类列表请。 我有一个简单的 SpringBoot 应用程序,其中有几个我自己的 @RestContr
我正在使用 spring 4.2 创建一些restfull web 服务。 但我们意识到,当用户输入错误的非强制性 @RequestParam 之一时,我们没有得到他传递的参数未知的错误。 就像我们有
我正在尝试向服务器发送一个 http 帖子,但我从我的 Controller 收到一个格式错误的 url 异常 Controller 代码 public static final String RES
我有一个抽象类PluginTemplate,由3个子类继承 PluginTemplateA、PluginTemplateB、PluginTemplateC。 我有一个其他类,其中有一个包含 Plugi
是否可以在Java配置(带有@Configuration注释的类)单独定义一个Spring RestController(@RestController注释的类)在标有 @Bean 的方法中)? 我有
我在使用 REST 服务器和 REST 客户端的应用程序中使用 Spring Boot。当我让人们选择 URL 映射时,就会出现问题,因为那时我需要使用此 URL 映射动态创建 REST Contro
我一直在尝试扩展这个 Spring/Hibernate/JPA sample code on Github 虽然我让它工作正常,但我正在尝试添加一个新的@RestController 以获取所有 sh
我正在使用 spring-rest 创建一些 @RestController servlet。该应用程序不在网络服务器上运行,而是作为带有嵌入式 tomcat 的简单命令行工具运行。 它们中的大多数应
是否有可能在 Springboot 中有两个不同的 @RestControllers 使用不同的 MappingJackson2HttpMessageConverter ? ...还是 Mapping
我正在尝试使用新的 Spring 4.0 @RestController 从 Controller 返回一个简单的文本响应: @RestController @RequestMapping(value
考虑以下代码: @RestController @RequestMapping("/timeout") public class TestController { @Autowired
嗨,我的示例中有一个简单的 RestController: @RestController public class PersonController { @RequestMapping(na
tl; dr一个RestController如果在Docker容器中运行,则可以正确回答,而另一个则不能。 该服务有两个API alive @CrossOrigin(origins = "*", ma
我知道有很多question s 涵盖了该主题,但我不知道如何实现以下要求。 我想上传文件列表,每个文件都包含一些额外信息。在 Java 世界中,这意味着以下内容: @NoArgsConstructo
是否可以通过编程方式控制@RestController来启用或禁用它?我不想只在每个 @RequestMapping 方法中编写代码来执行某种 if (!enabled) { return 404Ex
背景 这是@RestController中定义的方法,它从磁盘读取文件然后流回。 @RequestMapping(value = "/bill", method = RequestMethod.GET
我无法将完整的对象序列化为 Java API Rest。某些属性未在响应方法中返回。 我有一个像这样的对象: public class Localization { private Long
我有一个 Controller : @RestController @RequestMapping(value = UserRestController.REST_URL, produces = Me
我正在 Spring 中设置 RestController,但有一个不明确的映射问题。我不明白最后两个方法有什么歧义,因为请求映射和方法名称不同。当我从最后一个方法中删除方法规范时,问题就不再存在了。
我正在使用 Spring Boot 和 @RestController 以及 Java 8 实现 REST API。其中一个 Controller 方法需要调用另一个第 3 方 REST API 服务
我是一名优秀的程序员,十分优秀!