为什么
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实例是平等的.
我对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) 在我的控制器中,我有一个创建实体的方法
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,但没有注释?或者通过继承以某种方式可能?
我使用 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) 给定
// 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)
我不想生成每个功能a0,a1...,并将其分配给a独立。相反,我想生成函数并将它们分配给a一个循环,类似这样的东西(对不起的代码,它不会编译):
for (int i = 0; i != n; ++i)
{
void b() { cout << i; };
a[i] = b;
}
Run Code Online (Sandbox Code Playgroud)
这可能吗?我该怎么做?
在 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) 我想运行一个应该在完成工作后退出的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) 使用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.
我能做什么?这个电话的正确语法是什么?
在我的单元测试中,我们发现
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)
这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 ×6
spring ×4
spring-mvc ×3
hibernate ×2
postgresql ×2
spring-web ×2
arrays ×1
c++ ×1
datetime ×1
function ×1
java-8 ×1
java-time ×1
javascript ×1
jpa ×1
json ×1
pointers ×1
postman ×1
spring-boot ×1