点击我的Rest应用程序的/
目录不会重定向到我所访问的目录,而只是在屏幕上打印重定向指令:“ redirect:swagger-ui.html
”
我的控制器:
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
@RestController
class HomeController(val info: InfoProperties) {
@RequestMapping("/")
fun home(): String {
return "redirect:/swagger-ui.html"
}
}
答案 0 :(得分:2)
使用curl,我们看到的答案是文本(Content-Type:text / plain; charset = UTF-8),因此简单的意外文本输出:
> curl -v "http://localhost:8080/"
* Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8080 (#0)
> GET / HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.54.0
> Accept: * / *
>
< HTTP/1.1 200 OK
< Connection: keep-alive
< Content-Type: text/plain;charset=UTF-8
< Content-Length: 25
< Date: Fri, 15 Mar 2019 17:52:32 GMT
<
* Connection #0 to host localhost left intact
redirect:/swagger-ui.html
@RestController
注释是控制器的专用版本。它包含@Controller
和@ResponseBody
批注,而@ResponseBody
是导致我们出现问题的原因。
要解决此问题,请将@RestController
注释替换为更通用的@Controller
一个:
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.stereotype.Controller
@Controller
class HomeController(val info: InfoProperties) {
@RequestMapping("/")
fun home(): String {
return "redirect:/swagger-ui.html"
}
}
重定向现在可以正常工作了。
答案 1 :(得分:1)
您实际上可以实现此目的而无需将@RestController
更改为@Controller
。您需要做的是返回RedirectView
而不是字符串。这就是我在Java中工作的方式:
@RestController
@ApiIgnore
public class ApiDocsRedirectController {
@RequestMapping(value = {"/","/api-docs","/v3/api-docs"})
public RedirectView redirect() {
return new RedirectView("/swagger-ui.html");
}
}