我想从我的控制器发送JSON.我有以下配置.
spring-servlet.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<mvc:annotation-driven/>
<mvc:resources mapping="/resources/**" location="/resources/"/>
<context:component-scan base-package="com.castle.controllers"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
Run Code Online (Sandbox Code Playgroud)
.js:
function testAjax() {
var data = {userName: "MyUsername", password:"Password"};
$.ajax({
url: 'ajax/test.htm',
dataType : 'json',
type : 'POST',
contentType: "application/json",
data: JSON.stringify(data),
success: function(response){
alert('Load was performed.');
}
});
}
Run Code Online (Sandbox Code Playgroud)
UserTest.java:
public class UserTest {
private String userName;
private String password;
public String getUserName() …Run Code Online (Sandbox Code Playgroud) 我在实体中有这样的字段:
private String userId;
private String username;
private Date created;
private List<Comment> comments = new ArrayList<>();
Run Code Online (Sandbox Code Playgroud)
Comment 有这样的领域:
private String userId;
private String username;
private String message;
private Date created;
Run Code Online (Sandbox Code Playgroud)
我需要进行聚合,并收到类似这样的内容:
{
"userId" : "%some_userId%",
"date" : "%some_date%",
"commentsQty" : 100,
"isCommented" : true
}
Run Code Online (Sandbox Code Playgroud)
我的聚合看起来像这样:
{ "aggregate" : "%entityName%" , "pipeline" : [
{ "$project" : {
"username" : 1 ,
"userId" : 1 ,
"created" : 1 ,
"commentQty" : { "$size" : [ "$comments"]}}}]}
Run Code Online (Sandbox Code Playgroud)
它工作正常.但我还需要检查,IF注释数组包含一些注释,具体的userId.我尝试了这个,但它无法执行:
{ "aggregate" …Run Code Online (Sandbox Code Playgroud)