1

i have a POJO class BookType as:

package com.jersey.series.json.service;

import java.util.HashMap;
import java.util.Set;

public class BookType {

@Override
public String toString() {
    return "BookType [bookId=" + bookId + ", bookName=" + bookName + ", author=" + author + ", category=" + category
            + ", obj=" + obj + "]";
}

protected int bookId;
protected String bookName;
protected String author;
protected String category;
protected HashMap<String, Set<String>> obj = new HashMap<>();

public HashMap<String, Set<String>> getObj() {
    return obj;
}

public void setObj(HashMap<String, Set<String>> obj) {
    this.obj = obj;
}


public int getBookId() {
    return bookId;
}


public void setBookId(int value) {
    this.bookId = value;
}


public String getBookName() {
    return bookName;
}


public void setBookName(String value) {
    this.bookName = value;
}


public String getAuthor() {
    return author;
}


public void setAuthor(String value) {
    this.author = value;
}


public String getCategory() {
    return category;
}


public void setCategory(String value) {
    this.category = value;
}

}

And a rest Service :

 package com.jersey.series.json.service;


import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("/bookservice")
public class BookServiceImpl implements IBookService {



@POST
@Path("addbookandreturnjson")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public BookType saveAndReturnBookInfojson(BookType bookType) {

    return bookType;
}
}

i ma making this call using PostMan:

http://localhost:8080/Jersey-JSON-IO/rest/bookservice/addbookandreturnjson

this is my message body with type application Json

 {"bookId":5,"bookName":"abc","author":"saurabh","category":"Test","obj":{"1":["abc","xyz"]}}

Service is receiving everything correct except for the "obj" field where am i doing wrong or what i need to do.I am using Glassfish 4.1.1 as web container.Here is my pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>in.bench.resources</groupId>
<artifactId>Jersey-JSON-IO</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>Jersey-JSON-IO</name>
<description>Jersey web service using JSON (Jackson parser) as request/response</description>
<!-- https://jersey.java.net/documentation/latest/media.html#json.jackson -->

<!-- Jersey repository -->
<repositories>
    <repository>
        <id>snapshot-repository.java.net</id>
        <name>Java.net Snapshot Repository for Maven</name>
        <url>https://maven.java.net/content/repositories/snapshots/</url>
        <layout>default</layout>
    </repository>
    <repository>
        <id>prime-repo</id>
        <name>PrimeFaces Maven Repository</name>
        <url>http://repository.primefaces.org</url>
        <layout>default</layout>
    </repository>
</repositories>

<properties>
    <jersey.version>2.12</jersey.version>
    <jersey.scope>compile</jersey.scope>
    <compileSource>1.7</compileSource>
    <maven.compiler.target>1.7</maven.compiler.target>
    <maven.compiler.source>1.7</maven.compiler.source>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
    <finalName>Jersey-JSON-IO</finalName>

    <!-- maven compiler plugin -->
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>


    </plugins>
</build>

<dependencies>
        <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet-core</artifactId>
        <version>${jersey.version}</version>
        <scope>${jersey.scope}</scope>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>${jersey.version}</version>
        <scope>${jersey.scope}</scope>
    </dependency>

</dependencies>

saurabhiitr
  • 47
  • 1
  • 7

0 Answers0