在网络上,我发现了许多示例,其中使用 Orika 映射框架将来自一个源对象的字段映射到如下所示的目标对象。
mapperFactory.classMap(BasicPerson.class, BasicPersonDto.class)
.field("name", "fullName")
.field("age", "currentAge")
.register();
Run Code Online (Sandbox Code Playgroud)
但是我的要求和这个传统的映射不同。我得到两个源对象和一个目标对象。我需要将第一个源对象的一些字段和第二个源对象的一些字段映射到目标对象。
请发布您对这种情况的建议。
我在我的“预订”表上收到了一个很长的查询,它的效果很好,除非我询问代理。
我们有一张“用户”表。该表包含用户、管理员、代理等。
“预订”总是有一个“用户”,因此 $lookup 总是顺利进行。
“预订”有时有一个“代理”,但大多数时候该字段为空白“”。因此,当我执行 $lookup 时,它会破坏整个查询并且不返回任何内容。
我想做 $lookup,但前提是“代理”字段不为空。或者找到一种方法,以便如果 $lookup 失败,它不会破坏整个查询。
左侧是实际上有一个包含有效 users._id 的“agent”字段 - 这里我们得到结果
右侧是“代理”字段缺失、包含空白值或包含无效值的情况。- 这里它破坏了整个查询。
这是一个带有数据的示例来尝试一下
db.getCollection('booking').aggregate([
{
$match: {
property: "001",
checkin: {$gte: 1483596800},
checkout: {$lte: 1583596800}
}
},
{
$lookup: {
from: "users",
localField: "user",
foreignField: "_id",
as: "users"
}
},
{ $unwind: "$users" },
{
$lookup: {
from: "users",
localField: "agent",
foreignField: "_id",
as: "agent"
}
},
{ $unwind: "$agent"}
])
booking Table
{
"_id" : "AAAAA",
"property" : "001",
"user" : "U001", …Run Code Online (Sandbox Code Playgroud) 我有一个带有几个方法的控制器类,其中一个方法应该接收POST请求,并从该POST请求的主体中使用JSON创建一个新Account。
当我尝试使用curl发出POST请求时,出现错误消息
{"timestamp":1493988808871,"status":400,"error":"Bad Request","exception":"org.springframework.http.converter.HttpMessageNotReadableException","message":"Required request body is missing: org.springframework.http.ResponseEntity<?> com.example.AccountRestController.add(java.lang.String,java.lang.String)","path":"/users/add"}
Run Code Online (Sandbox Code Playgroud)
我正在使用的curl命令
curl -X POST --data '{"userName":"bepis", "password":"xyz"}' -H "Content-Type:application/json" http://localhost:8080/users/add
Run Code Online (Sandbox Code Playgroud)
AccountRestController
@RequestMapping(method = RequestMethod.POST, value = "/add", produces = { MediaType.APPLICATION_JSON_VALUE})
ResponseEntity<?> add(@RequestBody String username, @RequestBody String password) {
Account result = accountRepository.save(new Account (username, password));
return new ResponseEntity<>(result, HttpStatus.CREATED);
}
Run Code Online (Sandbox Code Playgroud) 我已经创建了一个Spring Boot应用程序,这就是我的控制器的外观。我正在使用邮递员在请求正文中发送json,并在请求标头中发送一个字符串,然后进一步对json进行哈希处理,并将其与请求标头中获得的字符串进行比较。问题是我不知道为了使用MockMvc测试相应的Controller类而获取请求主体和请求标头。
控制器逻辑
@RestController
public class Comparison {
@PostMapping(path = "/test")
public boolean compareHash(@RequestBody String json,
@RequestHeader(value = "code") String oldHashValue) {
Hash hashObj = new Hash();
String newHashValue = hashObj.sha512(json);
return oldHashValue.equals(newHashValue);
}
}
Run Code Online (Sandbox Code Playgroud)
测试逻辑
public class ComparisionTest {
@Autowired
private WebApplicationContext wac;
private MockMvc mockMvc;
@Before
public void setup () {
DefaultMockMvcBuilder builder = MockMvcBuilders.webAppContextSetup(this.wac);
this.mockMvc = builder.build();
}
@Test
public void contextLoads() throws Exception {
RecordedRequest recordedRequest = server.takeRequest();
}
}
Run Code Online (Sandbox Code Playgroud)
请在上面的代码中帮助我,以从请求中检索主体和标头值,并将hash(body)与标头值等效
我有以下注释:
@Repeatable(Infos.class)
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.Type, ElementType.Constructor})
public @interface Info {
String[] value() default {};
}
Run Code Online (Sandbox Code Playgroud)
如您所见,它是可重复的,并且使用的是包装器类Infos;
@Retention(RetentionPolicy.RUNTIME)
public @interface Infos {
Info[] value();
}
Run Code Online (Sandbox Code Playgroud)
但是我在Info类上遇到以下编译器错误;
容器注释的目标不是此注释的目标的子集
此错误的原因和解决方法是什么?
我将实体UserInfoEntity、变量名称从“moblie”更改为“mobile”,然后重新启动服务器。当我查看表格时,我发现表格没有删除“移动”列。这是我的实体变化;
由此;
@Entity
@Table(name = "pe_userinfo")
public class UserInfoEntity {
private String moblie;
}
Run Code Online (Sandbox Code Playgroud)
对此;
@Entity
@Table(name = "pe_userinfo")
public class UserInfoEntity {
private String mobile;
}
Run Code Online (Sandbox Code Playgroud) 我想制作一个包含重复元素的数组.例如:
var myArray = ["one", "two", "five"];
Run Code Online (Sandbox Code Playgroud)
当我循环遍历for循环时:
for(var i = 0; i < myArray.length; i++){
myArray.push(myArray[i]);
}
Run Code Online (Sandbox Code Playgroud)
我的浏览器崩溃了!我没有得到任何有意义的错误.有人可以解释为什么会这样吗?
我有一个.bat文件,其工作是查找Java版本。bat文件中编写的命令是java -version(这只是一个示例,不建议获取Java版本的其他方法)
运行.bat文件的代码:
String path = "cmd /c start d:\\java.bat";
Runtime rn = Runtime.getRuntime();
Process pr = rn.exec(path);
Run Code Online (Sandbox Code Playgroud)
bat文件正在运行,但正在循环运行。但是预期的行为是它应该打开命令提示符并仅运行一次命令
我有一个私有静态方法的主类.我想从另一个java类访问此方法.我试过一些方法,但是他们没有用.我该如何访问该方法?
像这样的主要班级;
public class RandomGenerate {
public static void main(String[] args) throws Exception {
System.out.print.ln("main method");
}
private static synchronized void createRandom(PersonObj person, int number, List s) {
System.out.println("deneme");
}
}
Run Code Online (Sandbox Code Playgroud)
我想createRandom从另一个像这样的java类调用;
public class Deneme {
RandomGenerate rg = new RandomGenerate();
RandomGenerate.createRandom(person, number, sList);
}
Run Code Online (Sandbox Code Playgroud)
然后,netbeans显示方法具有私有访问权限.
我正在寻找使用 Spring Data 提供的 API 实现对 Mongo Collection 的upsert操作的正确方法。
详细来说,我有以下用例。集合的架构collection如下所示:
{
_id: "some_id",
field1: "value1",
field2: "value2",
subdocument1: {
// A subdocument with some fields
},
subdocument2: {
// A subdocument with some other fields
}
}
Run Code Online (Sandbox Code Playgroud)
字段field1和field2始终存在,但subdocument1和subdocument2将在不同时刻插入:一个在第一次插入期间,第二个在随后的更新期间。
我看到MongoTemplate有和upsert方法。使用这种方法我必须构建自己的更新操作。
Query query = Query.query(Criteria.where("_id").is("some_id"));
Update.update("_id", "some_id")
.set("field1", "value1")
.set("field2", "value2")
.set("subdocument1", subdocumentObject);
mongoTemplate.upsert(query, update, Collection.class);
Run Code Online (Sandbox Code Playgroud)
我不明白这是否是我正在寻找的以及是否有更好的方法。
我想更新表中的特定记录,并且我使用主键(empId)和另一列(empSalary)来标识特定列。我们知道主键(empId)足以识别记录。但就我而言,如果该员工工资为 1000,我想更新记录。是否有任何选项可以使用实体而不是查询来更新记录?我正在使用 Spring Data JPA
例子:
public class Employee {
@Id
@Column
private int empId;
@Column
private String name;
@Column
private float salary;
@Column
private String state;
// getters, setters
}
Run Code Online (Sandbox Code Playgroud)
如果 empId 和工资与表匹配,我想更新整个表。
Joiner这里初始化不正确吗?我有以下课程,当我尝试使用 加入示例paths数组时joiner,我得到了一个NullPointerException.
public class CPath {
private static final Joiner joiner = Joiner.on("/");
private String[] elements;
@Override
public String toString() {
return joiner.join(elements);
}
}
// main method
final String[] paths = {"a/b/c", "d", "", null, "e/f/g", "h/i", null, ""};
final CPath c3 = new CPath(paths);
c3.toString(); //<<<< NPE
Run Code Online (Sandbox Code Playgroud) 我需要创建一个名为SecureHeaderCreatorcreated 的接口Entry<X, Y>
java ×10
hibernate ×2
mongodb ×2
spring ×2
spring-boot ×2
annotations ×1
arrays ×1
batch-file ×1
cmd ×1
for-loop ×1
generics ×1
guava ×1
javascript ×1
junit ×1
mockmvc ×1
orika ×1