英文:
Changing httpservletrequest to restcontroller in Spring
问题
我有一个servlet类,其中有一个从HttpServlet重写的方法“process”。
@override
protected void process(HttpServletRequest request, HttpServletResponse response) {
InputStream in = null;
OutputStream out = null;
String inXml = null;
//一些其他的代码...
}
它正在读取进入servlet的所有内容。
我如何在Spring中将其重写为RestController?
英文:
I have a servlet class which has a method "process", over ridden from HttpServlet
@override
protected void process(HttpServletRequest request, HttpServletResponse response) {
InputStream in = null;
OutputStream out = null;
String inXml = null;
//some more code..
}
It is reading whatever is coming into the servlet.
How can I rewrite this as rest controller in spring??
答案1
得分: 1
只需将其编写为:
@RestController
public ReturnType process(HttpServletRequest request, HttpServletResponse response {
//...
}
并且还要查看Spring MVC文档的此部分:
> @RequestMapping
处理程序方法具有灵活的签名,可以从支持的控制器方法参数和返回值范围中进行选择。
请注意:
- 从Rest-Controller返回的任何内容都将转变为HTTP响应正文;
- 您可以在类级别上定义
@RestController
。
英文:
Just code it as:
@RestController
public ReturnType process(HttpServletRequest request, HttpServletResponse response {
//...
}
and check this part of the Spring MVC documentation as well:
> @RequestMapping
handler methods have a flexible signature and can choose from a range of supported controller method arguments and return values.
Note, that:
- whatever you return from your Rest-Controller turns into HTTP Response Body;
- you can define
@RestController
on the class level.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论