Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Spring引导Rest中将GET请求参数映射到DTO对象时出现问题_Java_Spring_Spring Restcontroller_Spring Rest - Fatal编程技术网

Java 在Spring引导Rest中将GET请求参数映射到DTO对象时出现问题

Java 在Spring引导Rest中将GET请求参数映射到DTO对象时出现问题,java,spring,spring-restcontroller,spring-rest,Java,Spring,Spring Restcontroller,Spring Rest,我已经用SpringREST应用程序创建了一个SpringBoot 这是我的控制器代码 @RestController public class SampleController { @RequestMapping(value = "/sample/get", method = RequestMethod.GET, produces = "application/json") @ResponseBody public Response getResponse(SampleDTO d

我已经用SpringREST应用程序创建了一个SpringBoot

这是我的控制器代码

@RestController
public class SampleController {

  @RequestMapping(value = "/sample/get", method = RequestMethod.GET, produces = "application/json")
  @ResponseBody
  public Response getResponse(SampleDTO dto) {
    Response response = new Response();

    response.setResponseMsg("Hello "+dto.getFirstName());

    return response;
  }
}
这是我的样品

public class SampleDTO {

  @JsonProperty("firstname")
  private String firstName;

  public String getFirstName() {
    return firstName;
  }

  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }
}
这是我的回应对象

public class Response {

  private String responseMsg;

  public String getResponseMsg() {
    return responseMsg;
  }

  public void setResponseMsg(String responseMsg) {
    this.responseMsg = responseMsg;
  }
}
当我尝试以这种方式访问服务时

我得到了这个预期的输出

{“responseMsg”:“你好,mvg”}

当我尝试以这种方式访问服务时

我得到这个输出

{“responseMsg”:“Hello null”}

我的问题是如何将请求参数中的“firstname”映射为DTO的“firstname”


提前感谢

首先,您需要选择需要(或想要使用)参数或模型的方法。当您使用这样的
http://localhost:8080/sample/get?firstName=mvg
,您正在将数据作为请求参数传递。因此,您需要使用@RequestParam注释

使用@RequestParam注释的示例(使用说明见)


设置@JsonProperty(“firstname”)时,请确保已导入此语句“import com.fasterxml.jackson.annotation.JsonProperty;”。还有一件事,如果您要发送更多属性,而您的bean类没有,那么您可以在bean名称的顶部设置@JsonIgnoreProperties(ignoreUnknown=true)


您还缺少@RequestBody注释。您应该在getResponse方法中将其作为(@RequestBody SampleDTO dto)。

只需创建一个Pojo Java Bean,其中的字段名称与您的请求参数匹配

然后将该类用作请求处理程序方法的参数(无任何附加注释)


我需要知道如何处理DTO对象?将其映射到@modeldattribute-url对我有效:(.请再试一次。这部分“@modeldattribute(“pet”)pet-pet”如何解决我的问题?您能详细说明一下吗?首先您需要选择您需要(或想要使用)的方法param或model。当您使用类似于此的内容时,您将数据作为参数传递。因此您需要使用@RequestParam注释。我在读取HTTP消息时遇到此错误:org.springframework.HTTP.converter.httpMessageneTradableException:缺少必需的请求正文:public com.sample.dto.Response com.sample.controller.SampleController.getResponse(com.sample.dto.SampleDTO)当我在您的方法中的
SampleController
中添加此@RequestBody时,在
SampleDTO
参数中添加此注释@RequestBody时,我收到此错误,无法读取HTTP消息:org.springframework.HTTP.converter.httpmessageontradableexception:缺少必需的请求体:public com.sample.dto.Response com.sa当我添加这个@requestbody时,您没有从客户端/jsp正确修复json
    @RequestMapping(method = RequestMethod.GET)
public Response foo(@RequestParam("firstName") String firstName) {
    Response response = new Response();
    response.setResponseMsg("Hello "+ firstName );
    return response;
}