解组json @POST参数

时间:2016-07-14 10:31:23

标签: java json post unmarshalling javax.ws.rs

我的班级看起来像这样:

public class Test {

    @JsonProperty("name")
    private String name;

    public Test(){}

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

还有一个使用javax.ws.rs的端点类:

@Path("/Test")
public class Endpoint {

    @POST
    @Consumes("application/json")
    @Produces("application/json")
    @HeaderParam("Accept") @DefaultValue("application/json")
    public Response createTest(String json) {
        return Response.status(Response.Status.ACCEPTED).entity(json).build();
    }
}

这可以按预期工作。

问题:如何让createTest()将“Test”类作为参数而不是“String”?

试用:当使用“Test”作为createTest()的参数时,发送以下内容会显示“415 Unsupported Media Type”:

POST ../Endpoint/ HTTP/1.1
Host: localhost:8080
Accept: application/json
Content-Type: application/json
Cache-Control: no-cache
Postman-Token: 56d06798-2b32-57fc-c22a-0240aceb8f98

{
  "name": "Kalle"
}

1 个答案:

答案 0 :(得分:0)

我认为你必须使用Jersey 2.xx版本。

public class LoginRequest {

private String userName;
private String password;

public LoginRequest(){
    super();
}

public String getUserName() {
    return userName;
}

public void setUserName(String userName) {
    this.userName = userName;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

}

你的Bean资源(这是你的Bean测试)

@POST
@Path("login")
@Consumes(MediaType.APPLICATION_JSON)
public Response login(LoginRequest request) {
....
}

和家属:

<properties>
    <jersey.version>2.22.1</jersey.version>
    <jackson.version>2.6.1</jackson.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<version>1</version>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey</groupId>
            <artifactId>jersey-bom</artifactId>
            <version>${jersey.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>

    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>

    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.4</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet-core</artifactId>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-common</artifactId>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>${jackson.version}</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>${jackson.version}</version>
    </dependency>

</dependencies>

415不支持的媒体类型问题的原因是您没有正确的依赖性。

......玩得开心