带RequestBody的POST请求不起作用

时间:2019-07-13 17:08:19

标签: java spring spring-boot spring-annotations

在以下示例中,我正在使用POST和GET方法。 post是初始化一个变量,而GET是得到这个变量。 我用邮递员发出请求。

我收到错误

@RequestBody(value = "val") //cant resolve method value

请让我知道如何解决belwo错误,以便我可以使用post方法进行初始化并获取方法来检索值

Controller1

@Controller
@RequestMapping("/call1")
public class Call1 {

public String str = "inti";

@RequestMapping(value = "/intiparam1", method = RequestMethod.POST)
public void intiParam1(@RequestBody(value = "val") String val) {
    this.str = val;
}

@RequestMapping(value = "/getparam1", method = RequestMethod.GET)
public String getParam1() {
    return this.str;
}
}

2 个答案:

答案 0 :(得分:0)

何时使用@RequestBody

您不能在其中使用值。当您有多个要执行操作的字段实体时,可以使用此功能。假设您要保存用户,那么可能需要先创建用户模型并在控制器@RequestBody中使用。

型号:

public class User
{
   @Id
   private int id;
   private String firstname;
   private String lastname;
   //Getters-Setters, AllArgConstructor-constructor
}

@RequestMapping(value = "/requestBodyExample", method = RequestMethod.POST)
public String intiParam1(@RequestBody User user) {
     return user.getFirstname();
}

Quick Start with Spring Boot

答案 1 :(得分:0)

创建一个类Variable,并在控制器中使用其他代码。

CREATE OR REPLACE FUNCTION dummy_function()
RETURNS TABLE 
(
    word_id bigint,
    tag_id bigint
)
AS $$
DECLARE
    WORD_ID bigint;
    LEMMAS varchar(255)[];
    TAG_ID bigint;
BEGIN
    FOREACH word IN words LOOP
        LEMMAS = string_to_array(QuestionList_, ':')

        FOREACH lemma in ARRAY LEMMAS LOOP
        -- HERE I want to return word_id and tag_id that I would later insert into relations table
            RETURN QUERY word.id, SELECT tags.id FROM tags WHERE tag = lemma
        END LOOP
    END LOOP
    RETURN
END;
$$ LANGUAGE PLPGSQL;

发出请求时,将json作为{“ data”:“ 12345”}

,然后在代码中使用@RequestBody变量v而不是String,因为它可以满足您的默认值要求,并且可以扩展代码,因为将来可以根据需要在现有变量中添加不同的属性。

相关问题