json - 如何在 jax-rs 的 post 请求中处理 Json 主体

标签 json jaxb jersey jax-rs restful-architecture

我有一个关于 JAX-RS 的项目(作业)。我正在使用 NetBeans、Jersey 和 Tomcat。

enter image description here

这是系统中主要对象的“用户”类。

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="user")
public class User {
    
    //@XmlElement
    //public int id ;
    @XmlElement
    public String username;
    @XmlElement
    public String fullname;
    @XmlElement
    public String gender;
    @XmlElement
    public String birthDate;
    
    public User(){
        
    }
    
    public User(String username,String fullname, String gender,String birthDate){
        
        //this.id = id;
        this.username = username;
        this.fullname = fullname;
        this.gender = gender;
        this.birthDate = birthDate;
    }
    
}

这是我的“JAXBContextResolver”类

import com.sun.jersey.api.json.JSONConfiguration;
import com.sun.jersey.api.json.JSONJAXBContext;
import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;
import javax.xml.bind.JAXBContext;


@Provider
public class JAXBContextResolver implements ContextResolver<JAXBContext>{
    
    private JAXBContext context;
    private Class[] types = {User.class};
    
    
    public JAXBContextResolver() throws Exception {
        
        this.context = 
        new JSONJAXBContext(  JSONConfiguration.mapped().build(), types); 
    }
                    
    @Override
    public JAXBContext getContext(Class<?> objectType) {
         for (Class type : types) {
             if (type == objectType) {
                 return context;
             }
         }
        
        return null;
        
    }
    
}

这是我在“UserService”类中的发布方法

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON) 
    public List<User> createNewUser(User tUser)  {
        List<User> list = new ArrayList<User>();
        
        list.add(tUser);
            
            
        
        return list;
    }

当我尝试使用 RESTClient(Firefox 附加组件)在本地主机中发布新用户时,我的请求正文是这样的 json 输入:

{"user":{"username":"blabla","fullname":"blabla","gender":"M","birthDate":"05.01.1978"}}

在 post 方法中(在 UserService 类中)变量“tUser”必须自动填充即将到来的输入吗? “tUser”变量在 Debug模式下显示其中的空元素,如下所示:

enter image description here

如果我知道错了,有人可以纠正我吗?为什么这个值显示为空?他们不能显示“blabla”-“blabla”-“M”-“05.01.1878”吗?你能帮帮我吗?

最佳答案

我解决了这个问题;在 JAXBContextResolver 类中,我更改了这样的方法:

public JAXBContextResolver() throws Exception {

        this.context = 
        new JSONJAXBContext(  JSONConfiguration.mapped().rootUnwrapping(false).build(), types); 
}

与第一个不同的是增加了“rootUnwrapping(false)”表达式。

关于json - 如何在 jax-rs 的 post 请求中处理 Json 主体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14436615/

相关文章:

java - 为对象显示 null -JSON- JAXB

java - JaxBMarshaller 中定义的自定义 CharacterEscapeHandler 在 Jetty 中有效,但在 Weblogic 中无效

php - 如何使用 mysql 和 php 选择多行?

php - 如何获取 PHP 脚本以将数据馈送到 JQuery 自动完成中?

java - 解码 WADL 时遇到问题

JAVA RESTFull服务器获取POST数据,其中 "%"(百分比)作为值@FormParam

rest - 对多个PATH使用Single Jersey REST类

java - 带有 asynchttpclient 的 JSON 帖子

javascript - json 转键值数组

javascript - 来自 localstorage 的 JSON 无法在 php 中解码