如何在不破坏Spring的情况下将Spring Boot设置为基本包的主方法

时间:2016-06-25 02:52:09

标签: java spring-boot

我正在查看Spring Boot,我印象非常深刻。非常快速的发展。我有一个应用程序,但是,所有示例都有控制器包中的主要方法。 [例如。 com.demo]我想将控制器移动到自己的包中。 [例如。 com.demo.controller。然后将main方法保留在基础包[com.demo]中。当我这样做时,它打破了世界。似乎可能需要分离一些注释,或者可能需要拆分便利方法。我已经将我的Application.java移动到一个基础包中,因为我说过,并且移动了我认为正确的注释,但似乎我错过了一些东西。这是我的代码。

package com.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import com.demo.controllers.StudentController;

@SpringBootApplication
@EnableAutoConfiguration
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(StudentController.class, args);
    }
}

我的控制器如下

package com.demo.controllers;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.demo.models.Student;

@Controller
@RequestMapping("/student")
public class StudentController {

    @RequestMapping(method = RequestMethod.GET)
    public List<Student> getAll() {
        return new ArrayList<Student>();
    }

    @RequestMapping(method = RequestMethod.POST)
    public Student create(@RequestBody Student Student) {
        return null;
    }

    @RequestMapping(method = RequestMethod.DELETE, value = "{id}")
    public void delete(@PathVariable String id) {

    }

    @RequestMapping(method = RequestMethod.PUT, value = "{id}")
    public Student update(@PathVariable String id, @RequestBody Student Student) {
        return null;
    }
}

我得到的堆栈跟踪就是这个。

org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:133) ~[spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:532) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) ~[spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
at com.demo.Application.main(Application.java:14) [classes/:na]
Caused by: org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.getEmbeddedServletContainerFactory(EmbeddedWebApplicationContext.java:185) ~[spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:158) ~[spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:130) ~[spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
... 8 common frames omitted

不确定我是否缺少注释或需要拆分一些便利注释。

3 个答案:

答案 0 :(得分:2)

不是SpringApplication.run(StudentController.class, args);而是SpringApplication.run(Application.class, args);

请仔细遵循指南。

答案 1 :(得分:1)

在主类中,它不是调用控制器类调用应用程序类,而是解决了这个问题。

$.ajax()

@SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } 不是必需的,因为@AutoConfiguration包含内部

答案 2 :(得分:-1)

您的Application类必须如下所示:

function meu_ajax_scripts(){    
    wp_enqueue_script( 'meuajax', get_template_directory_uri().'/consulta.js', array('jquery'), '1.0', true );
    wp_localize_script( 'meuajax', 'meuajax', array( 'ajaxurl' =>   admin_url( 'admin-ajax.php' ) ) );
}
add_action('wp_enqueue_scripts', 'meu_ajax_scripts');

function prancha(){ 

    if(!empty($_POST)){
     $nome = $_POST['nome'];
     $email = $_POST['email'];
     $estilo = $_POST['estilo'];
     $experiencia = $_POST['experiencia'];
     $altura = $_POST['altura'];
     $peso = $_POST['peso'];

     cadastra_usuario($nome, $email, $estilo, $experiencia, $altura, $peso);
    }

    function cadastra_usuario($nome, $email, $estilo, $experiencia, $altura, $peso){          

         global $wpdb;

        $table = 'dados_usuario';

        $data = array(      
          'nome' => $nome,
          'email' => $email,
          'estilo' => $estilo,
          'experiencia' => $experiencia,
          'altura' => $altura,
          'peso' => $peso,
        );

        $updated = $wpdb->insert( $table, $data );

        if ( ! $updated ) {
          $wpdb->print_error();
        }

    }
    die(); 
  }

  add_action('wp_ajax_prancha', 'prancha');
  add_action('wp_ajax_nopriv_prancha', 'prancha');