Spring Boot-使用MySql访问构建RESTful Web服务

时间:2018-12-31 16:46:37

标签: spring rest api spring-boot

我向rest-api发送了一个将对象存储到mysql数据库的请求。

我可以通过Jpa将对象存储到数据库中的哪个步骤丢失了?

这里是Rest-Controller

@RestController
public class OwnerRestController {
    @Autowired
    private final OwnerRestRepository repo;

    public OwnerRestController(OwnerRestRepository repo) {this.repo = repo;}

    @RequestMapping(value="/owner/add", method=RequestMethod.POST)
    public Owner create(@RequestBody Map<String, String> body){
        Owner o = new Owner();
        o.setFirstName(body.get("firstName"));
        o.setLastName(body.get("lastName"));
        o.setAddress(body.get("address"));
        o.setCity(body.get("city"));
        o.setTelephone(body.get("telephone"));
        this.repo.save(o);
        return o;
    }
}

这是存储库接口

public interface OwnerRestRepository extends CrudRepository<Owner,integer>{}

这是JSON对象所有者

{
    "firstName":"fname",
    "lastName":"lname",
    "address":"address1",
    "city":"city1",
    "telephone":"4711"
}

服务器响应

{
    "id": 11,
    "firstName": "fname",
    "lastName": "lname",
    "address": "address1",
    "city": "city1",
    "telephone": "4711"
}

无法将数据存储在数据库中的代码有什么问题?

最诚挚的问候,Mux

1 个答案:

答案 0 :(得分:1)

默认情况下,createStore运行内存数据库-import store "../exported/from/somewhere"; function* startFacebookRegister({payload}) { const { id, email, first_name, last_name } = payload; yield put(initFacebookLogin.success(res)); } function graphRequestWrapper() { const infoRequest = new GraphRequest( '/me', { httpMethod: 'GET', version: 'v2.5', parameters: { 'fields': { 'string': 'email,first_name,last_name,id' } } }, (err, response) => { store.dispatch({type: "INIT_FACEBOOK_LOGIN_REQUEST_SUCCESS_SAGA", payload: {...response}}) }); new GraphRequestManager().addRequest(infoRequest).start(); } function* mainSaga() { yield takeLatest("INIT_FACEBOOK_LOGIN_REQUEST_SUCCESS_SAGA", startFacebookRegister); yield call(graphRequestWrapper); } ,因此您的项目仅在应用程序运行时存在。
要保留数据,您需要将应用程序配置为使用其他数据库。

要查询Spring,您还可以使用The H2 Console Application

例如,如果要配置H2,则应在H2文件中添加以下配置选项。

MySQL

如果您使用的是application.properties

spring.datasource.url=jdbc:mysql://localhost/test
spring.datasource.username=dbuser
spring.datasource.password=dbpass
spring.datasource.driver-class-name=com.mysql.jdbc.Driver