gpt4 book ai didi

java - MessageBodyReader 支持 DTO 类吗?

转载 作者:行者123 更新时间:2023-12-02 06:28:24 24 4
gpt4 key购买 nike

我想使用 Grails-jaxrs plugin实现自定义 MessageBodyReaderSupport从客户端读取 UserDto 类。

如何实现 UserDtoReader 才能获取 UserDto 的实例?

这是我的 UserDto 类:

public class UserDto {
private String firstName;
private String lastName;

public UserDto() {
firstName = "";
lastName = "";
}

public UserDto(String firstName,
String lastName) {

this.firstName = firstName;
this.lastName = lastName;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}


@Override
public String toString() {
String s = " { User ";
s += "id=" + id + " ";
s += "firstName=" + firstName + " ";
s += "lastName=" + lastName + " ";
s += " User } ";
return s;
}
}

这是我的 UserDtoReader 类:

@Consumes("application/json")
class UserDtoReader extends MessageBodyReaderSupport<UserDto> {

@Override
public UserDto readFrom(MultivaluedMap<String, String> httpHeaders,
InputStream entityStream) throws IOException,
WebApplicationException {
// TODO Auto-generated method stub
return null;
}
}

最佳答案

使用这个:

@Consumes("application/json")
class UserDtoReader extends MessageBodyReaderSupport<UserDto> {

@Override
public UserDto readFrom(MultivaluedMap<String, String> httpHeaders,
InputStream entityStream) throws IOException,
WebApplicationException {
return new JsonSlurper().parse(new InputStreamReader(entityStream))
}
}

关于java - MessageBodyReader 支持 DTO 类吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20301665/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com