1. 程式人生 > >SpringBoot頁面不跳轉問題

SpringBoot頁面不跳轉問題

第一次使用:@RestController時,一直返回字串,未跳轉至指定頁面。

原因分析:

@RestController註解由@[email protected]兩個註解派生而來,

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Controller
@ResponseBody
public @interface RestController {

	/**
	 * The value may indicate a suggestion for a logical component name,
	 * to be turned into a Spring bean in case of an autodetected component.
	 * @return the suggested component name, if any (or empty String otherwise)
	 * @since 4.0.1
	 */
	@AliasFor(annotation = Controller.class)
	String value() default "";

}

解決方法:

@ResetController修改為@Controller即可跳轉頁面。

提醒:

@Responsebody後,返回結果直接寫入HTTP response body中,不會被解析為跳轉路徑。

使用@RestController返回json資料就不需要在方法前面加@ResponseBody註解了。