春季启动与AngularJS [英] Spring Boot with AngularJS

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

问题描述

我在使用作为泽西我的REST服务,并使用我的前端开发AngularJS一个Spring引导项目。当我运行它,而无需使用任何控制器,并转到index.html的(这是在资源/静态/ index.html的),它工作正常。当我添加它呈现一个控制器给出字符串的index.html作为输出。春天引导配置:

 进口org.springframework.boot.SpringApplication;
进口org.springframework.boot.autoconfigure.EnableAutoConfiguration;
进口org.springframework.boot.autoconfigure.SpringBootApplication;
进口org.springframework.context.annotation.ComponentScan; @SpringBootApplication
 @ComponentScan(basePackages = {\"com.cst.interfaces\",\"com.cst.configuration\",\"com.cst.application\",\"com.cst.application.implmentation\"})
 @EnableAutoConfiguration
 公共类ApplicationConfiguration {
    公共静态无效的主要(字符串ARGS [])抛出异常{
        SpringApplication.run(ApplicationConfiguration.class,参数);
    }
    公共ServletRegistrationBean jerseyServlet(){
        ServletRegistrationBean注册=新ServletRegistrationBean(新ServletContainer(),/ *);
        register.addInitParameter(ServletProperties.JAXRS_APPLICATION_CLASS,JerseyInitalize.class.getName());
        返回登记;
    }
}

JerseyConfiguration:

 进口org.glassfish.jersey.server.ResourceConfig;
进口org.springframework.stereotype.Component;
@零件
公共类JerseyInitalize扩展ResourceConfig {
    公共JerseyInitalize(){
        超();
        this.packages(com.cst.interfaces);
    }
}

控制器类:

 进口javax.ws.rs.GET;
进口javax.ws.rs.Path;
进口javax.ws.rs.Produces;
进口org.springframework.web.bind.annotation.RestController;
@RestController
@Path(/家)
公共类HomeResource {
    @得到
    @Produces(应用/ JSON)
    公共字符串的getString(){
        返回的index.html
    }
}


解决方案

这是因为你标注了 @RestController 控制器,这是<一个href=\"https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/RestController.html\"相对=nofollow>的简写获得 @Controller @ResponseBody 。后者注解指示控制器来呈现输出原样直接进入反应

使用 @Controller 的控制器,是的的REST风格代替。

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;
    }
}

JerseyConfiguration:

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");
    }
}

Controller Class:

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";
    }
}

解决方案

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.

Use @Controller for controllers that are not RESTful instead.

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

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