小编Ali*_*ani的帖子

带Jetty的AbstractAnnotationConfigDispatcherServletInitializer

我正在使用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)

java spring spring-mvc embedded-jetty

9
推荐指数
1
解决办法
1395
查看次数

Spring Boot和Thymeleaf 3.0.0.RELEASE集成

我有一个问题,当我尝试集成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)

java spring thymeleaf spring-boot

8
推荐指数
2
解决办法
2万
查看次数

MongoDB插入文档"或"增量字段(如果存在于数组中)

我尝试做的很简单,我在文档中一个数组 ;

"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中做到这一点?

mongodb

8
推荐指数
1
解决办法
982
查看次数

什么是Spring4.0.3中AuthenticationException.getAuthentication()的替代品

我在我的项目中使用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)也已弃用.这两种方法有其他选择吗?

java spring spring-security

8
推荐指数
1
解决办法
803
查看次数

响应中的Spring rest api filter字段

我正在使用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序列化程序来动态过滤.

请分享一些想法.

rest spring json filter

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

删除Transfer-Encoding:在POST请求中分块?

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)

java spring spring-mvc

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

检测RefreshScope bean的刷新

我的理解是,当您使用Spring Cloud的RefreshScope注释时,会注入数据的代理,如果更改了支持信息,代理会自动更新.不幸的是,我需要找到一种方法,以便在刷新时发出警报,以便我的代码可以重新读取刷新范围的bean中的数据.

简单示例:计划任务,其计划存储在Cloud Config中.除非你等到下一次执行任务(可能需要一段时间)或定期轮询配置(这看起来很浪费),否则无法知道配置是否已更改.

java spring spring-boot spring-cloud

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

Spring Boot @ExceptionHandler隐藏异常名称

我正在使用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)

java spring spring-mvc spring-boot

7
推荐指数
3
解决办法
2523
查看次数

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
查看次数

在spring boot中通过动态键读取属性

我想知道Spring Boot中是否有任何方法可以使用动态密钥从属性文件中读取属性值.我知道属性可以放入application.properties并且可以使用@Value("propertyKey")但是我的键将是动态的.

我知道@PropertySource要读取属性值,我可以动态构造我的键.那么有没有Spring Boot提供的方法?

java spring spring-boot

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