我正在使用Jetty 9.1.0.RC2和Spring 4.有一个AbstractAnnotationConfigDispatcherServletInitializer
并尝试启动初始化:
Server server = new Server();
WebAppContext webAppContext = new WebAppContext();
webAppContext.setContextPath("/");
webAppContext.setConfigurations(new Configuration[] { new AnnotationConfiguration() });
webAppContext.setParentLoaderPriority(true);
webAppContext.setAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern", ".*/target/classes/.*");
server.setHandler(webAppContext);
server.start();
server.join();
Run Code Online (Sandbox Code Playgroud)
但未能发现:
No Spring WebApplicationInitializer types detected on classpath
Run Code Online (Sandbox Code Playgroud) 我有一个问题,当我尝试集成Spring Boot 1.3.5.RELEASE和Thymeleaf 3.0.0.释放.我知道Spring Boot现在支持Thymeleaf 3版本所以我尝试解决这个问题,如下所示:
@SpringBootApplication(exclude={org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration.class})
Run Code Online (Sandbox Code Playgroud)
并添加我自己的SpringWebConfig配置.不幸的是收到这样的错误:
java.lang.ClassNotFoundException: org.thymeleaf.resourceresolver.IResourceResolver
at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_66]
at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_66]
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) ~[na:1.8.0_66]
at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_66]
... 37 common frames omitted
Wrapped by: java.lang.NoClassDefFoundError: org/thymeleaf/resourceresolver/IResourceResolver
wrapped by: java.lang.IllegalStateException: Could not evaluate condition on org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration due to org/thymeleaf/resourceresolver/IResourceResolver not found. M ake sure your own configuration does not rely on that class. This can also happen if you are @ComponentScanning a springframework package (e.g. if you put a @ComponentScan in the default package by mistake)
Run Code Online (Sandbox Code Playgroud) 我尝试做的很简单,我在文档中有一个数组 ;
"tags": [
{
"t" : "architecture",
"n" : 12
},
{
"t" : "contemporary",
"n" : 2
},
{
"t" : "creative",
"n" : 1
},
{
"t" : "concrete",
"n" : 3
}
]
Run Code Online (Sandbox Code Playgroud)
我想将一个项目数组推送到数组
["architecture","blabladontexist"]
Run Code Online (Sandbox Code Playgroud)
如果item存在,我想增加对象的n
值(在这种情况下是它architecture
),
如果不这样做,请将其添加为新项目(值为n=0
){ "t": "blabladontexist", "n":0}
我已经试过$addToSet
,$set
,$inc
,$upsert: true
有这么多的组合,不能做到这一点.
我们怎样才能在MongoDB中做到这一点?
我在我的项目中使用Spring 3,现在升级到Spring 4.0.3.RELEASE.现在在使用时AuthenticationException.getAuthentication()
,它表示已弃用,但无法找到备选方案.这是代码:
public ModelAndView init(HttpServletRequest request, HttpServletResponse response) {
AuthenticationException exception = (AuthenticationException) request.getSession().getAttribute(WebAttributes.AUTHENTICATION_EXCEPTION);
Authentication loginAuthentication = exception.getAuthentication();
// Set the user name for the change password screen
return new ModelAndView("common/changePassword", "userName", loginAuthentication.getPrincipal());
}
Run Code Online (Sandbox Code Playgroud)
该方法setAuthentication(authentication)
也已弃用.这两种方法有其他选择吗?
我正在使用spring rest api 4.x. 我们需要根据请求参数过滤响应中的字段.
我的用户对象:
private class UserResource {
private String userLastName;
private String userFirstName;
private String email;
private String streetAddress;
}
E.g. URL: curl -i http://hostname:port/api/v1/users?fields=firstName,lastName.
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我只需要返回请求参数"fields"中的字段.JSON输出应仅包含firstName,lastName.
有几种方法可以根据对象过滤Jackson中的字段.在我的情况下,我需要通过将字段列表传递给Jackson序列化程序来动态过滤.
请分享一些想法.
我POST
使用以下代码发送请求,但请求以chunked(Transfer-Encoding: chunked
)的形式发送.我搜索了这个问题,并说它包含Content-Length
但在下面的代码中我无法弄清楚如何设置Content-Length
:
@RequestMapping(value = "/contacts", method = RequestMethod.POST)
public Map<String, ContactInfo> addContactInfo(
@RequestBody Map<String, ContactInfo> ContactInfoDto) {
ContactInfo contactInfo = ContactInfoDto.get("contact");
if (contactInfo == null) {
throw new IllegalArgumentException("Contact not found.");
}
contactInfo = this.contactInfoManager.addNew(contactInfo);
Map<String, ContactInfo> map = new HashMap<>();
map.put("contact", contactInfo);
return map;
}
Run Code Online (Sandbox Code Playgroud) 我的理解是,当您使用Spring Cloud的RefreshScope
注释时,会注入数据的代理,如果更改了支持信息,代理会自动更新.不幸的是,我需要找到一种方法,以便在刷新时发出警报,以便我的代码可以重新读取刷新范围的bean中的数据.
简单示例:计划任务,其计划存储在Cloud Config中.除非你等到下一次执行任务(可能需要一段时间)或定期轮询配置(这看起来很浪费),否则无法知道配置是否已更改.
我正在使用Spring Boot 1.3.X并具有以下内容:
@RestController
@RequestMapping(path = "/foo")
public class FooController {
@RequestMapping(method = RequestMethod.GET, params = { "fooBar" })
public Collection<Entry> firstFoo() {
//Do something
}
@RequestMapping(method = RequestMethod.GET, params = { "anotherFooBar" })
public Collection<Entry> secondFoo() {
//Do something other
}
}
Run Code Online (Sandbox Code Playgroud)
哪个按预期工作.传递错误的参数时,会引发以下异常:
{
"error": "Bad Request",
"exception": "org.springframework.web.bind.UnsatisfiedServletRequestParameterException",
"message": "Parameter conditions \"fooBar\" OR \"anotherFooBar\" not met for actual request parameters: elementalFormulae={C11}",
"path": "/foo",
"status": 400,
"timestamp": 1455287433961
}
Run Code Online (Sandbox Code Playgroud)
然后我创建了ExceptionHandler
如下所示:
@ControllerAdvice
public class ExcptionController {
@ResponseStatus(value=HttpStatus.BAD_REQUEST, reason="Invalid …
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 Boot中是否有任何方法可以使用动态密钥从属性文件中读取属性值.我知道属性可以放入application.properties
并且可以使用@Value("propertyKey")
但是我的键将是动态的.
我知道@PropertySource
要读取属性值,我可以动态构造我的键.那么有没有Spring Boot提供的方法?
spring ×9
java ×8
spring-boot ×4
spring-mvc ×4
filter ×1
json ×1
mongodb ×1
rest ×1
spring-cloud ×1
spring-web ×1
thymeleaf ×1