简体   繁体   English

如何在没有来自其他类的嵌套对象的情况下返回JSON对象?

[英]How can I Return a JSON Object without Nested Objects From Other Classes?

I want to return a JSON Object with information from two different Classes. 我想返回一个JSON对象,其中包含来自两个不同类的信息。 Like I want the username from the class User and the rolename from the Class Role together in one JSON Object. 就像我想要来自User类的用户名和来自Class Rolerolename一起放在一个JSON对象中。

My current code: 我目前的代码:

@Entity
@DynamicUpdate
public class User {
    private String username;
    private String phone;
    private String email;
    private Set<Role> role;
}

@Entity
public class Role {
    private int idRole;
    private String name;
}

@Projection(name = "CustomUser", types = {User.class})
public interface CustomUser {
    String getUsername();
    RoleTest getRole();

    interface RoleTest {
        String getName();
    }
}

@Repository
public interface UserRepository extends JpaRepository<User, Integer> {
    List<CustomUser> findAllBy();
}

@Controller
@RequestMapping
public class UserController  {
    @Autowired
    private UserRepository userRepository;
    @GetMapping
    @ResponseStatus(HttpStatus.ACCEPTED)
    public @ResponseBody List<CustomUser> getAllUsers() {
        return userRepository.findAllBy();
    }
}

What I currently get: 我目前得到的:

{ 
   "role": {
       "name": "ADMIN"
   },
   "username": "test" 
}

However, my goal is it to get something like this: 但是,我的目标是得到这样的东西:

{ 
  "role": "ADMIN",
  "username": "test"
}

I think you could simply do the following. 我想你可以简单地做以下事情。

@Projection(name = "CustomUser", types = {User.class})
public interface CustomUser {

    String getUsername();

    @Value("#{target.getRoles(0).getName()}") 
    String getRole();
}

You're getting the role -> name structure because RoleTest is an interface and it might have multiple values. 您将获得角色 - >名称结构,因为RoleTest是一个接口,它可能有多个值。

If you are using the Jackson library, please check @JsonUnwrapped. 如果您使用的是杰克逊图书馆,请查看@JsonUnwrapped。 The documentation is here 文档在这里

The problem here is that @JsonUnwrapped doesn't work with the Collections. 这里的问题是@JsonUnwrapped不适用于集合。 As you indicated in one of your comments if the Role need not be a set, this will solve your problem. 正如您在其中一条评论中指出的那样,如果角色不必是一套,这将解决您的问题。 If you have questions on why the @JsonUnwrapped doesn't work with the Collcetions, this will help to understand further. 如果您对@JsonUnwrapped不能与Collcetions一起使用有疑问, 将有助于进一步理解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用其他类中的对象? - How can I use objects in other classes? 嵌套类的对象如何访问它们嵌套的对象? - How can objects of a nested classes access the object that they're nested in? 从其他json对象创建json对象 - create json object from other json objects 在其他 json 对象中直接获取特定的嵌套 json 对象 - getting directly a specific nested json object inside other json objects 如何将每个整数/双精度值从包含嵌套对象和 arrays 的 JSON 数据转换为字符串? - How can I convert each integer/double value to String from JSON data containing nested objects and arrays? 如何在Java中返回可被其他对象访问的数组? - How can i return an array in java that is accessible by other objects? 如何从相互连接的对象行的末端删除对象? (Java) - How can I remove an object from the end of a line of objects connected to each other? (Java) 如何将 FlightOfferSearch 和 FlightOrder 对象作为 JSON 返回? - How can I return FlightOfferSearch and FlightOrder objects as JSON? 如何加载一个 ArrayList,我可以从其他类访问和操作而无需多次加载它们? - How do I load an ArrayList that I can acces and manipulate from other classes without loading them more than once? 如何防止Eclipse导入嵌套类? - How can I prevent Eclipse from importing nested classes?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM