小编Joh*_*gel的帖子

为什么ZoneOffset.UTC!= ZoneId.of("UTC")?

为什么

ZonedDateTime now = ZonedDateTime.now();
System.out.println(now.withZoneSameInstant(ZoneOffset.UTC)
        .equals(now.withZoneSameInstant(ZoneId.of("UTC"))));
Run Code Online (Sandbox Code Playgroud)

打印出来false

我希望这两个ZonedDateTime实例是平等的.

java datetime java-8 java-time

100
推荐指数
1
解决办法
7万
查看次数

Spring Web MVC:HTTP DELETE不再有请求体

我对Spring Web MVC的开发人员提出了一个问题.

简而言之:之前可以在HTTP DELETE消息中发送请求正文,但现在又不可能了.为什么?

详细地:

我们正在使用spring-webmvc-4.2.4.RELEASE.

@RestController
public class Controller {

    @RequestMapping(value = "/{pathVariable}/deleteAnything", method = RequestMethod.DELETE)
    public ResponseEntity<?> deleteAnything(@PathVariable String pathVariable,
        @Valid @RequestBody Set<Pojo> pojoSet) {
        ...
Run Code Online (Sandbox Code Playgroud)

我们发送

DELETE /anything/deleteAnything HTTP/1.1
Content-Type: application/json
Host: example.com

[ {
  "any field" : "Any value"
} ]
Run Code Online (Sandbox Code Playgroud)

并得到例外

m.m.a.RequestResponseBodyMethodProcessor : Read [java.util.Set<packagename.Pojo>] as "application/json;charset=UTF-8" with [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@333825a3]
.w.s.m.m.a.ServletInvocableHandlerMethod : Error resolving argument [1] [type=java.util.Set]
HandlerMethod details: 
Controller [packagename.Controller]
Method [public org.springframework.http.ResponseEntity<?> packagename.Controller.deleteAnything(java.lang.String,java.util.Set<packagename.Pojo>)]


org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public org.springframework.http.ResponseEntity<?> …
Run Code Online (Sandbox Code Playgroud)

java spring spring-mvc spring-web

7
推荐指数
2
解决办法
3133
查看次数

Spring Rest Controller:如何有选择地关闭验证

在我的控制器中,我有一个创建实体的方法

import javax.validation.Valid;
...
@RestController
public class Controller {

  @RequestMapping(method = RequestMethod.POST)  
  public ResponseEntity<?> create(@Valid @RequestBody RequestDTO requestDTO) {
  ...
Run Code Online (Sandbox Code Playgroud)

import org.hibernate.validator.constraints.NotEmpty;
...
public class RequestDTO
    @NotEmpty // (1)
    private String field1;
    //other fields, getters and setters.
Run Code Online (Sandbox Code Playgroud)

我想添加一个控制器方法

update(@Valid @RequestBody RequestDTO requestDTO)
Run Code Online (Sandbox Code Playgroud)

但是在这个方法中应该允许为field1空或为空,即行

@NotEmpty // (1)
Run Code Online (Sandbox Code Playgroud)

RequestDTO应该被忽略.

我怎样才能做到这一点?我是否必须编写一个看起来完全相同的类RequestDTO,但没有注释?或者通过继承以某种方式可能?

java spring spring-mvc hibernate-validator spring-web

7
推荐指数
1
解决办法
1676
查看次数

JPA:如何更改主键中的列顺序

我使用 Spring Data JPA 和 PostgreSQL 10。

鉴于课程

@Entity
@Table(name = "test_table")
@IdClass(TestTableId.class)
public class TestTable {
    @Id
    private int b;
    @Id
    private int a;
    private int c;
    // Getters, setters, hashCode() and equals()
}
Run Code Online (Sandbox Code Playgroud)

public class TestTableId implements Serializable {
    private int b;
    private int a;
    // Constructors, getters, setters, hashCode() and equals()
}
Run Code Online (Sandbox Code Playgroud)

在数据库中,表是由 hibernate 通过创建的

CREATE TABLE test_table
(
    a integer NOT NULL,
    b integer NOT NULL,
    c integer NOT NULL,
    CONSTRAINT test_table_pkey PRIMARY KEY (a, …
Run Code Online (Sandbox Code Playgroud)

java postgresql hibernate jpa spring-data-jpa

5
推荐指数
1
解决办法
2031
查看次数

C ++:如何在循环中生成数组以函数指针

给定

// from an external C api
void f(int i, void (*g)());

const int n = ...
void a0(); void a1(); ...
void (*a[n])();

int main()
{
  a[0] = a0; a[1] = a1; ...
  for (int i = 0; i != n; ++i)
    f(i, a[i]);
  ...
}
Run Code Online (Sandbox Code Playgroud)

我不想生成每个功能a0a1...,并将其分配给a独立。相反,我想生成函数并将它们分配给a一个循环,类似这样的东西(对不起的代码,它不会编译):

for (int i = 0; i != n; ++i)
{
    void b() { cout << i; };
    a[i] = b;
}
Run Code Online (Sandbox Code Playgroud)

这可能吗?我该怎么做?

c++ arrays pointers function

5
推荐指数
1
解决办法
194
查看次数

PostgreSQL, Spring Data JPA: Integer null 解释为 bytea

在 PostgreSQL 我有表

CREATE TABLE public.my_table
(
    id integer NOT NULL,
    ...
Run Code Online (Sandbox Code Playgroud)

我想执行查询:向我显示具有给定 ID 的行。如果 id 为空,则显示所有行。

我试过了

public interface MyRepository extends JpaRepository<MyTable, Integer> {

    @Query(value = "SELECT * FROM my_table WHERE (?1 IS NULL OR id = ?1)", nativeQuery = true)
    List<MyTable> findAll(Integer id);
Run Code Online (Sandbox Code Playgroud)

如果id != null,一切都很好。但是如果id == null,我会收到错误

org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:261) ~[spring-orm-4.3.13.RELEASE.jar:4.3.13.RELEASE]
...
Caused by: org.hibernate.exception.SQLGrammarException: could not extract …
Run Code Online (Sandbox Code Playgroud)

postgresql hibernate spring-data-jpa

5
推荐指数
1
解决办法
3980
查看次数

Spring Boot:退出应用程序时出现异常

我想运行一个应该在完成工作后退出的Spring应用程序.但在我的实现中,我得到了一个例外.

build.gradle 包含:

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web") {
        exclude module: "spring-boot-starter-tomcat"
    }
}
Run Code Online (Sandbox Code Playgroud)

Application.java:

@SpringBootApplication
public class Application {

    @Autowired
    private ApplicationContext context;

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

    @PostConstruct
    public void doTheWorkAndExit() {
        // do the work ...
        SpringApplication.exit(context, () -> 0);
    }

}
Run Code Online (Sandbox Code Playgroud)

我得到了例外

Exception thrown from LifecycleProcessor on context close

java.lang.IllegalStateException: LifecycleProcessor not initialized - call 'refresh' before invoking lifecycle methods via the context: org.springframework.context.annotation.AnnotationConfigAppl
icationContext@1807f5a7: startup date [Fri Mar 11 10:03:27 CET 2016]; …
Run Code Online (Sandbox Code Playgroud)

java spring spring-boot

4
推荐指数
1
解决办法
1127
查看次数

邮递员:如何评估json数组

使用Postman,可以将响应主体中的特殊字段保存到变量中,并在连续调用中使用此变量的值.

例如:在我第一次调用webservice时,在响应正文中返回以下内容

[ {
  "id" : "11111111-1111-1111-1111-111111111111",
  "username" : "user-1@example.com",
}, {
  "id" : "22222222-2222-2222-2222-222222222222",
  "username" : "user-2@example.com"
} ]
Run Code Online (Sandbox Code Playgroud)

我添加了一个测试

postman.setGlobalVariable("user_0_id", JSON.parse(responseBody)[0].id);
Run Code Online (Sandbox Code Playgroud)

现在我使用URL向webservice发送连续请求

http://example.com/users/{{user_0_id}}
Run Code Online (Sandbox Code Playgroud)

邮差评估{{user_0_id}}11111111-1111-1111-1111-111111111111.

这很好用.但现在我加入了我的第一次电话测试

postman.setGlobalVariable("users", JSON.parse(responseBody));
Run Code Online (Sandbox Code Playgroud)

在我对webservice的第二次请求中,我调用了URL

http://example.com/users/{{users[0].id}}
Run Code Online (Sandbox Code Playgroud)

但现在{{users[0].id}}无法评估,它保持不变,不会被替换11111111-1111-1111-1111-111111111111.

我能做什么?这个电话的正确语法是什么?

javascript json postman

3
推荐指数
1
解决办法
1万
查看次数

Spring REST 文档:如何替换参数

在我的单元测试中,我们发现

this.mockMvc
  .perform(post("/authenticate")
    .contentType(MediaType.APPLICATION_FORM_URLENCODED)
    .param("username", "user@example.com")
    .param("password", "superSecretPassword"))
  .andExpect(status().isOk())
  .andDo(document("preprocessed-request",
    preprocessRequest(replacePattern(Pattern.compile("superSecretPassword"), "XXX"))));
Run Code Online (Sandbox Code Playgroud)

参见 Spring REST 文档文档

build/generated-snippets/preprocessed-request/http-request.adoc与内容一起生成

[source,http]
----
POST /authenticate HTTP/1.1
Content-Type: application/x-www-form-urlencoded

username=user%40example.com&password=superSecretPassword
----
Run Code Online (Sandbox Code Playgroud)

但我希望密码会因为 replacePattern() 被屏蔽:

[source,http]
----
POST /authenticate HTTP/1.1
Content-Type: application/x-www-form-urlencoded

username=user%40example.com&password=XXX
----
Run Code Online (Sandbox Code Playgroud)

我能做什么?

java spring spring-mvc spring-restdocs

2
推荐指数
1
解决办法
2178
查看次数