0

I am using spring data jpa with hibernate This is my dao interface

@Repository
public interface IUserDAO extends JpaRepository<User, Integer>{
    User findByUsername( final String username );
}

This is my User class

Entity
@Table(name="USER")
public class User {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name="ID", nullable = false)
  private int id;

  @Column(name="USERNAME", nullable = false)
  private String username;

  @Column(name="NAME", nullable = false)
  private String name;

  public int getId() {
    return id;
  }

  public void setId(int id) {
    this.id = id;
  }

  public String getUsername() {
    return username;
  }

  public void setUsername(String username) {
    this.username = username;
  }

  public String getName() {
    return name;
  }

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

}

This is my UserImplClass

This is my UserImplClass{
@Autowired
    private IUserDAO iUserDAO;

public String findUserByUserName(String username) {

        User user =iUserDAO.findByUsername(username);
        Convert user to json object from framework level automatically
        // i can add my one implemenation of converting user to  json here ,but i want to achieve it from  framework so that my code is not scattered on every service level
        return "jsonStringOfUserObject" 
    }

Is it possible with spring data jpa with hibernate so that i do not have to write code for converting java object to json string in every service level?

I am using spring ,therefore i want to achieve it from spring .

4
  • JSON for what? Are you asking about returning JSON for an HTTP request or for another purpose? Commented Mar 27, 2014 at 10:02
  • Yes for returning http response .But,the code in my case is outside of web service code.I am returning from prehandle .I am using spring rest.When returning from rest controller i do not have problem.But,when returning from prehandle ,i want to get json string when called to UserImplClass from prehandle class. Commented Mar 27, 2014 at 10:12
  • It's very unclear what you're trying to do. You want JSON before the Spring message converter handles the controller return value? Why? Why not just stay with type-safe objects until you need to serialize them for transmission over the network? Commented Mar 27, 2014 at 10:14
  • @chrylis, I agree with you. This is why I gave him an answer divided to two options. If he uses the second option, something is wrong at the design level of his application. Conversion to JSON should occur only prior to returning the data via HTTP Commented Mar 27, 2014 at 10:30

1 Answer 1

3

You have two options to do what you want:

1) If you plan on returning this Object as an HTTP Response, and you use Spring MVC with Controllers you can annotate your controller method as follows:

public @ResponseBody User getUser(){
   return userImplClass.findUserByUserName("yourusername");
}

2) If you want the UserImplClass itself to return a JSON String (which I do't recommend, but I leave you the decision), you can use Jackson Object Mapper to do it for you (you can inject it if you declare it as a bean on your configuration xml, or create a new instance of it, I personally prefer injecting it with @Autowired)

public String findUserByUserName(String username) {

    User user =iUserDAO.findByUsername(username);


    ObjectMapper mapper = new ObjectMapper(); // no need to do this if you inject via @Autowired


    return  mapper.writeValueAsString(user);
}
Sign up to request clarification or add additional context in comments.

2 Comments

But,i have to do this for every service.
If you find yourself using the second option (which I did gave you a working solution to), I believe something is wrong with your application design. On normal applications, the Service layer doesn't have to return JSON/any other presentation level data. It should return plain Java objects and the JSON conversion needs to occur on your Controller.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.