带有 AngularJS 的 Spring Boot [英] Spring Boot with AngularJS

查看:27
本文介绍了带有 AngularJS 的 Spring Boot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Spring Boot 项目,使用 Jersey 作为我的 REST 服务并使用 AngularJS 进行前端开发.虽然我在不使用任何控制器的情况下运行它并转到 index.html(在 resource/static/index.html 中),但它工作正常.当我添加一个控制器时,它会呈现字符串index.html"作为输出.Spring Boot 配置:

I have a Spring Boot project using Jersey as my REST service and using AngularJS for my front end development. While I run it without using any controller and go to index.html (which is in resource/static/index.html) it works fine. When I add a controller it renders gives the string "index.html" as an output. Spring Boot Configuration:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

 @SpringBootApplication
 @ComponentScan(basePackages = {"com.cst.interfaces","com.cst.configuration","com.cst.application","com.cst.application.implmentation"})
 @EnableAutoConfiguration
 public class ApplicationConfiguration {
    public static void main(String args[]) throws Exception{
        SpringApplication.run(ApplicationConfiguration.class, args);
    }
    public ServletRegistrationBean jerseyServlet(){
        ServletRegistrationBean register = new ServletRegistrationBean(new ServletContainer(),"/*");
        register.addInitParameter(ServletProperties.JAXRS_APPLICATION_CLASS, JerseyInitalize.class.getName());
        return register;
    }
}

球衣配置:

import org.glassfish.jersey.server.ResourceConfig;
import org.springframework.stereotype.Component;
@Component
public class JerseyInitalize extends ResourceConfig{
    public JerseyInitalize(){
        super();
        this.packages("com.cst.interfaces");
    }
}

控制器类:

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import org.springframework.web.bind.annotation.RestController;
@RestController
@Path("/home")
public class HomeResource {
    @GET
    @Produces("application/json")
    public String getString(){
        return "index.html";
    }
}

推荐答案

这是因为您使用 @RestController 注释了您的控制器,即 @Controller 的简写,带有 <代码>@ResponseBody.后一个注释指示控制器将输出按原样直接渲染到响应中.

This is because you annotated your controller with @RestController, which is a shorthand for @Controller with @ResponseBody. The latter annotation instructs the controller to render the output as-is directly into the response.

@Controller 用于 RESTful 的控制器.

Use @Controller for controllers that are not RESTful instead.

这篇关于带有 AngularJS 的 Spring Boot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆