1

Here is my controller:

@Controller
@RequestMapping("api")
public class RestController {
    @RequestMapping(value="check",method=RequestMethod.GET,produces="application/json")
    @ResponseBody
    public UserAcc returnuser(){
        return UserDao.getUserById(1);
    }
}

... and this is my UserAcc class:

@XmlRootElement
@Entity
@Table(name = "user_acc")
@NamedQueries({
@NamedQuery(name = "UserAcc.findAll", query = "SELECT u FROM UserAcc u")})
public class UserAcc implements Serializable {
private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "user_id")
    private Integer userId;

    @Basic(optional = false)
    @Lob
    @Column(name = "user_name")
    private String userName;

    @Basic(optional = false)
    @Lob
    @Column(name = "Company")
    private String company;

    @Basic(optional = false)
    @Column(name = "user_email")
    private String userEmail;

    @Basic(optional = false)
    @Column(name = "is_account_activated")
    private boolean isAccountActivated;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "assigenedToId")
    private List<Issues> issuesList;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "assosiateId")
    private List<Projectdetails> projectdetailsList;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "moduleassignedtoid")
    private List<DevelAcc> develAccList;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "assignedUserId")
    private List<TestAcc> testAccList;

    // ... code ommitted

}

But i am not getting any json response from userAcc object. Is there any problem? My spring configuration is working fine. Any help is appreciated!

achingfingers
  • 1,629
  • 5
  • 22
  • 45
FullMetal
  • 38
  • 1
  • 6

3 Answers3

1

Assuming you have all the required dependencies (as already mentioned in Jack's answer) on your classpath, you could try it like this:

@Controller
@RequestMapping("/api")
public class RestController {
   @RequestMapping(value="/check",method=RequestMethod.GET,produces="application/json")
   public @ResponseBody UserAcc returnuser(){
     return UserDao.getUserById(1);;
   }
}

...or like that:

@Controller
@RequestMapping("/api")
public class RestController {
   @RequestMapping(value="/check",method=RequestMethod.GET,produces="application/json")
   public @ResponseBody Map<String, Object> returnuser(){
     UserAcc acc = UserDao.getUserById(1);
     Map<String,Object> map = new HashMap<>();
     map.put("id",""+acc.getId());
     //... and so on
     return map;
   }
}
achingfingers
  • 1,629
  • 5
  • 22
  • 45
1

You need to use a library (such as jackson) to convert UserAcc class to json. Refer to http://www.journaldev.com/2552/spring-restful-web-service-example-with-json-jackson-and-client-program for an example.

@XmlRootElement
public class UserAcc {
    .... fields
}
suman j
  • 5,788
  • 7
  • 48
  • 89
1

I implemented this recently using Jackson (which is used to convert your POJO responses into JSON). Firstly, I added Jackson dependencies to my project (I'm using Maven):

    <!-- Jackson for Java - JSON conversion -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>${jackson-2-version}</version>
    </dependency>

    <!-- Jackson for Java - JSON conversion -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>${jackson-2-version}</version>
    </dependency>

    <!-- Jackson for Java - JSON conversion -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>${jackson-2-version}</version>
    </dependency>

Spring will then automatically use Jackson to generate the JSON response for any Controller method that returns an "application/json" response (as is the case for you).

If you want to prepend your JSON response with some characters to prevent JSON hijacking, then include this in your Spring config file:

<mvc:annotation-driven>
    <!-- Set JSON converter (for converting Java to JSON in REST response --> 
    <mvc:message-converters>
        <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
            <!-- Add prefix to JSON response to prevent JSON hijacking -->
            <property name="prefixJson" value="true" />
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>
  • this will prepend all JSON responses with this character string: {} && (which should be stripped out by the calling client prior to processing the response).
Community
  • 1
  • 1
CodeClimber
  • 4,298
  • 8
  • 43
  • 53