restcontroller方法

vojdkbi0  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(385)

我已经创建了一个spring启动应用程序来运行spring批处理框架。但是不幸的是,没有一个restcontroller方法可以运行:
我的restcontroller类如下

@RestController
@RequestMapping(path = "/myapp")
public class MyBatchController {
  @Autowired
  private JobLauncher jobLauncher;

  @Autowired
  @Qualifier("myJob")
  private Job myJob;

   @PostMapping(value = "/invokeMyJob")
   @CrossOrigin
   public ResponseEntity<String> processStock(){
    JobExecution myJobExecution = null;
    ResponseEntity<String> myJobResponse = null;
    JobParameters jobParameters = new JobParametersBuilder().addLong("time", System.currentTimeMillis())
            .toJobParameters();
    try {
        myJobExecution = jobLauncher.run(myJob, jobParameters);
        myJobResponse = new ResponseEntity<String>(myJobExecution.getExitStatus().getExitCode(), HttpStatus.OK); 
    }catch(Exception e) {
        e.printStackTrace();
        myJobResponse = new ResponseEntity<String>("failure", HttpStatus.INTERNAL_SERVER_ERROR); 
      }
    return myJobResponse;
    }
 }

我的 Postman 回复

我的主要应用课程如下:

@SpringBootApplication
 @ComponentScan(basePackages = "{com.myapp.batch}")
 public class MybatchApplication {
    public static void main(String[] args) {
       SpringApplication.run(MybatchApplication.class, args);
   }
 }

我无法追踪我为什么在 Postman 里得到404???

dtcbnfnu

dtcbnfnu1#

首先, @SpringBootApplication 由三个注解组成: @ComponentScan , @EnableAutoConfiguration & @Configuration 如果您显式使用 @ComponentScan ,那么你可能无法使用 @SpringBootApplication .
替换 "{com.myapp.batch}" 为了这个 {"com.myapp.batch"} 修改为:

@EnableAutoConfiguration
 @Configuration 
 @ComponentScan(basePackages = {"com.myapp.batch"})
 public class MybatchApplication {
    public static void main(String[] args) {
       SpringApplication.run(MybatchApplication.class, args);
   }
 }

@SpringBootApplication(scanBasePackages = {"com.myapp.batch"})
public class MybatchApplication {

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

    }

}


如果 com.myapp.batch 那么,是基本包吗

@SpringBootApplication
public class MybatchApplication {

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

    }

}

相关问题