将httpservletrequest在Spring中改为restcontroller。

huangapple go评论75阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年10月7日 18:54:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/64242590.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定