Spring Boot Rest:通过邮递员发布JSON时找不到错误404

时间:2017-10-26 15:15:26

标签: rest spring-boot

我正在尝试通过邮递员调用POST服务。我的应用程序在嵌入式tomcat服务器上运行。但是,当我尝试调用该服务时,我得到的错误是“在DispatcherServlet中找不到带有URI [/ findrouting]的HTTP请求的映射,名称为'dispatcherServlet'” 它甚至不识别http:localhost:8080 /

RoutingRequest和RoutingResponse是带有getter和setter的POJO。 我在这里错过了一些东西。我确实检查了很多例子,但没有找到解决问题的方法。

请参阅下面的代码:

package com.ab.hello.ambassador.server;

import java.util.Arrays;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(AmbassadorApplication.class, args);

        System.out.println("List of Beans instantiated");
        String[] beanNames = ctx.getBeanDefinitionNames();
        Arrays.sort(beanNames);
        for (String beanName : beanNames) {
            System.out.println(beanName);
        }
    }
}


package com.ab.hello.ambassador.server.controller;

import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.dp.connect.ambassador.server.RoutingRequest;
import com.dp.connect.ambassador.server.RoutingResponse;

@RestController
public class ApplicationController {

    @RequestMapping("/")
    public String index() {
        return "Spring Boot POC Welcomes You!";
    }


    @RequestMapping(method = POST, value = "/findrouting", consumes = { APPLICATION_JSON_VALUE }, produces = {
            APPLICATION_JSON_VALUE })
    public RoutingResponse findRoute(@RequestBody RoutingRequest request) throws Exception {
        // some business logic that would return response; as of now I have set it to null
        RoutingResponse response = null;
        return response;
    }

}

4 个答案:

答案 0 :(得分:1)

感谢您的时间。经过相当长时间的挣扎。我想出了这段代码。添加了注释@EnableWebMvc。这解决了我的问题。

@SpringBootApplication
@EnableWebMvc
public class Application extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

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

}

答案 1 :(得分:0)

尝试在控制器中定义路径:

@RestController
@RequestMapping("/")
public class ApplicationController {

    @RequestMapping
    public String index() {
        return "Spring Boot POC Welcomes You!";
    }


    @RequestMapping(method = POST, value = "/findrouting", consumes = { APPLICATION_JSON_VALUE }, produces = {
            APPLICATION_JSON_VALUE })
    public RoutingResponse findRoute(@RequestBody RoutingRequest request) throws Exception {
        // some business logic that would return response; as of now I have set it to null
        RoutingResponse response = null;
        return response;
    }

}

答案 2 :(得分:0)

更改以下行

  

ApplicationContext ctx = SpringApplication.run(AmbassadorApplication.class,args);

  

ApplicationContext ctx = SpringApplication.run(Application.class,args);

答案 3 :(得分:0)

根据spring docs,似乎需要覆盖一个配置方法:

    @SpringBootApplication
public class Application extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

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

}