1

I'm kinda stuck on this topic.

This is what i already found out. A good tutorial was :

Using MySQL in Spring Boot via Spring Data JPA and Hibernate http://blog.netgloo.com/2014/10/27/using-mysql-in-spring-boot-via-spring-data-jpa-and-hibernate/

I also found some information how to make single page application with hsqldb. But i really want to create something that permanent saves the users data to the database using mysql.

But in order to use angular http i need json. Can i convert the urls like

/create?email=[email]&name=[name]

To json how should i proceed. Does anyone knows good tutorials on this. Or are there better way's to proceed.

Greg
  • 1,690
  • 4
  • 26
  • 52
  • 1
    Check jhipster project, it can generate for you a full stack angularjs+springboot with a database of your choice. https://jhipster.github.io/ – GUISSOUMA Issam Sep 16 '15 at 16:17

2 Answers2

0

if you have already managed to use it with HSQLDB, it's juste a matter of database properties (like the JDBC URL) and schema initialization.

Can you provide the code sample of the controller, how you save the data (via a Repository or a simple DAO ?) and the application.properties

Y.M.
  • 640
  • 1
  • 7
  • 17
0

The simplest/handy way to consuming JSON with Spring Boot is using a Java class that resembles your JSON (https://stackoverflow.com/a/6019761).

So, you can follow the tutorial you linked, then use a controller like this one to handle JSONs:

@RestController
public class UserController {

  @RequestMapping(
      value = "/user/create",
      method = RequestMethod.POST,
      consumes = MediaType.APPLICATION_JSON_VALUE)
  public ResponseEntity<?> createUser(@RequestBody User user) {

    try {
      // Handle the User object here
      userDao.save(user);
    }
    catch (Exception ex) {
      return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
    }

    return new ResponseEntity<>(HttpStatus.OK);
  }  

  // ...

}

Receiving a JSON like this (at the url /user/create):

{email: "john@doe.com", name: "John Doe"}

An user will be saved in your database.


Responding with JSON

Moreover, if you want to send a response as JSON from your controller you should create a java object then send it back as response, within the ResponseEntity object.

For example, suppose to have this class:

public class SuccessDto {

  private String success;

  public SuccessDto(String success) {
    this.success = success;
  }

}

You can change your controller in this way:

public ResponseEntity<SuccessDto> createUser(@RequestBody User user) {

  // ...

  return new ResponseEntity<>(
    new SuccessDto("true"),
    HttpStatus.OK
  );
}

and you will have this JSON as response

{success: "true"}
Community
  • 1
  • 1
Andrea
  • 15,900
  • 18
  • 65
  • 84