无法使用Spring RepositoryRestResource更新数据库记录

时间:2017-01-23 11:37:27

标签: spring-boot spring-data-rest

我正在尝试使用Spring Boot和Spring Data Rest来创建一个简单的CRUD休息服务,但我似乎无法让更新部分工作。

我正在使用spring-boot-starter-data-rest版本1.2.2.RELEASE。

这是我的模特:

@Entity
@Table(name = "person")
public class Person {
    @Id
    @Column(name = "id")
    private String _id;

    @Column(name = "first_name")
    private String _firstName;

    @Column(name = "last_name")
    private String _lastName;

    public String getId() {
        return _id;
    }

    public String getFirstName() {
        return _firstName;
    }

    public String getLastName() {
        return _lastName;
    }

    public void setId(String id) {
        _id = id;
    }

    public void setFirstName(String firstName) {
        _firstName = firstName;
    }

    public void setLastName(String lastName) {
        _lastName = lastName;
    }
}

这是我的存储库定义:

@RepositoryRestResource(path = "/person-repository")
public interface PersonRepository extends CrudRepository<Person, String> {
}

我的表定义如下:

create table person
(
   id varchar(64) primary key, 
   first_name varchar(64), 
   last_name varchar(64)
);

通过此设置,我可以查询存储库(首先获得空结果):

$ curl 'http://localhost:9999/uaa/person-repository' -H  "Authorization: Bearer $TOKEN"
{ }

顺便说一下,我在这里使用OAuth2,因此授权标头。这是我的应用程序主类:

@Configuration
@SpringBootApplication
@RestController
@EnableResourceServer
public class AuthserverApplication {
    @RequestMapping("/user")
    public Principal user(Principal user) {
        return user;
    }

    public static void main(String[] args) {
        SpringApplication.run(AuthserverApplication.class, args);
    }

    @Configuration
    @EnableAuthorizationServer
    protected static class OAuth2Config extends AuthorizationServerConfigurerAdapter {
        @Inject
        private AuthenticationManager _authenticationManager;
        @Autowired
        private DataSource _dataSource;
        @Autowired
        private JdbcTokenStore _tokenStore;

        @Override
        public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
            endpoints.authenticationManager(_authenticationManager).tokenStore(tokenStore());
        }

        @Override
        public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
            clients.jdbc(_dataSource);
        }

        @Bean
        public JdbcTokenStore tokenStore() {
            return _tokenStore;
        }

        @Bean
        protected AuthorizationCodeServices authorizationCodeServices() {
            return new JdbcAuthorizationCodeServices(_dataSource);
        }

    }
}

我还可以添加新记录:

$ curl 'http://localhost:9999/uaa/person-repository/' -X POST  -D - -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json"  -d '{ "id" : "12345", "firstName": "Stefan", "lastName": "Meier" }'
HTTP/1.1 201 Created
Server: Apache-Coyote/1.1
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-Frame-Options: DENY
X-Application-Context: application:9999
Location: http://localhost:9999/uaa/person-repository/12345
Content-Length: 0
Date: Mon, 23 Jan 2017 10:26:03 GMT

插入实际上有效:

$ curl 'http://localhost:9999/uaa/person-repository' -H  "Authorization: Bearer $TOKEN"
{
  "_embedded" : {
    "persons" : [ {
      "id" : "12345",
      "lastName" : "Meier",
      "firstName" : "Stefan",
      "_links" : {
        "self" : {
          "href" : "http://localhost:9999/uaa/person-repository/12345"
        }
      }
    } ]
  }
}

但是当我尝试更新现有记录(尝试在此处更改firstName)时,它不起作用:

$ curl 'http://localhost:9999/uaa/person-repository/12345' -X PUT  -D - -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json"  -d '{ "id" : "12345", "firstName": "Peter", "lastName": "Meier" }'
HTTP/1.1 204 No Content
Server: Apache-Coyote/1.1
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-Frame-Options: DENY
X-Application-Context: application:9999
Location: http://localhost:9999/uaa/person-repository/12345
Date: Mon, 23 Jan 2017 10:41:38 GMT


$ curl 'http://localhost:9999/uaa/person-repository' -H  "Authorization: Bearer $TOKEN"                                                               {
  "_embedded" : {
    "persons" : [ {
      "id" : "12345",
      "lastName" : "Meier",
      "firstName" : "Stefan",
      "_links" : {
        "self" : {
          "href" : "http://localhost:9999/uaa/person-repository/12345"
        }
      }
    } ]
  }
}

我没有发现错误,但数据保持不变。我也尝试使用PATCH代替PUT,但无济于事。

删除记录有效:

$ curl 'http://localhost:9999/uaa/person-repository/12345' -X DELETE  -D - -H "Authorization: Bearer $TOKEN"
HTTP/1.1 204 No Content
Server: Apache-Coyote/1.1
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-Frame-Options: DENY
X-Application-Context: application:9999
Date: Mon, 23 Jan 2017 10:42:28 GMT

$ curl 'http://localhost:9999/uaa/person-repository' -H  "Authorization: Bearer $TOKEN"
{ }

必须有一些我不知道的东西。

0 个答案:

没有答案