【问题标题】:How to use @produces in JSON object spring boot如何在 JSON 对象 Spring Boot 中使用 @produces
【发布时间】:2018-01-26 17:07:45
【问题描述】:

是否可以在 Spring Boot 中将 @produces 用于 JSON 对象?或者还有其他方法可以实现:

JSONObject J_Session = new JSONObject();
J_Session.put("SESSION_ID_J", session_jid);
J_Session.put("J_APP", "J");
J_Session.put("REST_ID_J", rest_id);

【问题讨论】:

  • 你能否详细说明问题,你只是想知道你是否可以使用@Produces注解在Spring rest API中给出json响应?
  • 使用 POJO。它们应该被默认支持。
  • @Chaitanya 是的,就是这样。你能举个例子吗
  • @peeskillet 你能根据我的帖子举个例子吗?
  • @Chaitanya 我是 java 新手,如何根据您的评论进行转换?

标签: json spring-boot annotations


【解决方案1】:

有一些错误,不要使用 JSONObject ,只有在必要时(尝试使用 Gson 库),为了约定变量 J_Session 的第一个字母必须像 jSession 一样小写,如果你的类有注释 @RestController 你仅当类标记为@Controller 时才需要在方法中使用@ResponseBody;有一个带有@Controller 注释而不是@RestController 注释的类的示例,有了这个你的JSONObject 不再是必需的,另一件事,尝试了解pojos 和bean 之间的差异,并在使用它们后总是尝试取消你的变量(对不起我的英语)

@RequestMapping(value = "/postMethod", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public List<String> insertSomething(@RequestBody String body){
    ArrayList<String> listOfStrings=null;
    if (body != null){
        listOfStrings= new ArrayList<String>();
        listOfStrings.add (body);
        listOfStrings.add("somethin else");//Json Object because the "produces"
    }else{
        listOfStrings = new ArrayList<String>();// empty Json object to avoid null exception in client side (in my case Angular 6)
    }

    return listOfStrings;
}

【讨论】:

    【解决方案2】:

    这是一个简单的例子:

    RestController class:
    
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    
    import com.websystique.springboot.model.User;
    
    @RestController
    @RequestMapping("/api")
    public class RestApiController {
    
        @RequestMapping(value = "/user/", method = RequestMethod.GET, produces = { "application/json" })
        public List<User> listAllUsers() {
            List<User> users = new ArrayList<User>();
            users.add(new User(1, "Sam", 30, 70000));
            users.add(new User(2, "Tom", 40, 50000));
            users.add(new User(3, "Jerome", 45, 30000));
            users.add(new User(4, "Silvia", 50, 40000));
            return users;
        }
    }
    

    produces = { "application/json" } 属性自动将 List 集合转换为 json 响应。

    下面是 POJO 类。

    User Pojo class
    
    
    public class User {
    
        private long id;
    
        private String name;
    
        private int age;
    
        private double salary;
    
        public User(){
        }
    
        public User(long id, String name, int age, double salary){
            this.id = id;
            this.name = name;
            this.age = age;
            this.salary = salary;
        }
    }
    

    示例 JSON 响应:

    [
       {
          "id":1,
          "name":"Sam",
          "age":30,
          "salary":70000
       },
       {
          "id":2,
          "name":"Tom",
          "age":40,
          "salary":50000
       },
       {
          "id":3,
          "name":"Jerome",
          "age":45,
          "salary":30000
       },
       {
          "id":4,
          "name":"Silvia",
          "age":50,
          "salary":40000
       }
    ]
    

    关注link 获取完整详细的 CRUD 操作示例。

    以上代码来自这个链接本身,我只是修改了控制器部分以使其简单。

    【讨论】:

    • 您的 Pojo 关键字结束了 2 小时调试为什么 Spring Boot 不生成 json 对象。我使用的是非 POJO 类。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-09
    • 2021-04-21
    • 2019-12-04
    • 2017-03-06
    • 2021-07-16
    • 2022-01-21
    相关资源
    最近更新 更多