Spring Rest Api无法识别的类型帖子的请求方法

时间:2017-04-23 12:17:31

标签: spring spring-boot spring-rest

本准则工作正常:

@RestController
@RequestMapping("/api/post")
public class PostController {

@Autowired
private PostService postService;

public PostService getPostService() {
    return postService;
}

public void setPostService(PostService postService) {
    this.postService = postService;
}

@RequestMapping(value = "/userPost", method = RequestMethod.GET)
@ResponseBody
public String userPost(String username) {
    try {
        List<Validation> validations = new ArrayList<Validation>();
        ValidationHandeler.rejectIfEmptyOrWhitespace(validations, username, "username is requierd");

        if (validations.isEmpty()) {
            List<Post> followers = getPostService().findUserPost(username);
            return new Message(followers, MessageSuccesStatusEnum.SUCCESS).toString();
        }

        return new Message(validations, MessageSuccesStatusEnum.FAILED).toString();

    } catch (Exception ex) {
        return new Message("Error retriving follower list : " + ex.toString(), MessageSuccesStatusEnum.FAILED)
                .toString();
    }
}

这是输出:

{"result":"0","info":["username is requierd"]}

但是当我将RequestMethod类型更改为post时,它会给我以下输出:

{"timestamp":1492948640973,"status":404,"error":"Not Found","message":"No message available","path":"/api/post/userPost"}

是否有人知道如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

看起来您正在使用GET而不是POST作为请求方法。

@RequestMapping(value =&#34; / userPost&#34;,method = RequestMethod.GET)

答案 1 :(得分:0)

<dependencies>


        <!-- https://mvnrepository.com/artifact/io.rest-assured/rest-assured -->
        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>rest-assured</artifactId>
            <version>4.1.2</version>
            <scope>test</scope>
        </dependency>


        <!-- https://mvnrepository.com/artifact/org.testng/testng -->
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>7.0.0</version>
            <scope>test</scope>
        </dependency>


        <!-- https://mvnrepository.com/artifact/io.rest-assured/json-schema-validator -->
        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>json-schema-validator</artifactId>
            <version>4.1.2</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.10.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api -->
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.3.1</version>
        </dependency>
    </dependencies>

TestClass TestNG
import org.testng.annotations.Test;

import io.restassured.RestAssured;
import static io.restassured.RestAssured.*;

import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
import io.restassured.http.ContentType;
import io.restassured.response.Response;

public class basics {

    @Test
    public void getPlaceAPI() 
    {
        RestAssured.baseURI = "https://maps.googleapis.com";
        given().param("location", "42.3675294,-71.186966").param("radius", 
        "10000").param("key", "").when()
        .get("/maps/api/place/textsearch/json").then().assertThat().statusCode(200);
    }
}