小编Ali*_*ani的帖子

在MongoDB中查找一组不同的字段

我在MongoDB中有以下JSON集合.

{
    "_id": ObjectId("57529381551673386c9150a6"),
    "team_code": 3,
    "team_id": 2
},
{
     "_id": ObjectId("57529381551673386c91514a"),
     "team_code": 4,
     "team_id": 5
},
{
   "_id": ObjectId("57529381551673386c91514b"),
   "team_code": 3,
   "team_id": 2
},
{
   "_id": ObjectId("57529381551673386c91514c"),
   "team_code": 4,
   "team_id": 5,
}
Run Code Online (Sandbox Code Playgroud)

从数据中可以看出,每个都有2个记录,每个记录带有(team_code=3, team_id =2)和(team_code =4, team_id=5).是否有可能获得一组独特的团队代码和团队ID.就像是 ,

{
 "team_code" : 3, "team_id" : 2,
 "team_code" : 4, "team_id" : 5,
}
Run Code Online (Sandbox Code Playgroud)

mongodb mongodb-query

4
推荐指数
1
解决办法
1414
查看次数

springboot:如何在HandlerInterceptor的预处理中返回错误状态代码

HandlerInterceptor在Spring Boot中使用它来处理常见的请求类型.但是在执行时preHandle,如果不满足条件,我想将错误状态代码返回给用户.如果我preHandle在响应中抛出异常将具有所有异常堆栈.如何使用preHandle的响应代码发送自定义正文作为响应

java spring spring-boot

4
推荐指数
1
解决办法
6130
查看次数

如何在Heroku上获取MongoDB版本号?

我不知道如何获取在Heroku上运行的MongoDB版本号?

heroku mongodb

3
推荐指数
1
解决办法
6113
查看次数

将HttpServletRequest中的remoteUser值传递给mockmvc执行测试

我有一个api电话:

@RequestMapping(value = "/course", method = RequestMethod.GET)
ResponseEntity<Object> getCourse(HttpServletRequest request, HttpServletResponse response) throwsException {
        User user = userDao.getByUsername(request.getRemoteUser());

}
Run Code Online (Sandbox Code Playgroud)

当我从测试类中调用它时,我得到的用户为null,如:

HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
 Mockito.when(request.getRemoteUser()).thenReturn("test1");

    MvcResult result =  mockMvc.perform( get( "/course")
                    .contentType(MediaType.APPLICATION_JSON)
                    .andExpect( status().isOk() )
                    .andExpect( content().contentType( "application/json;charset=UTF-8" ) )
                    .andReturn();
Run Code Online (Sandbox Code Playgroud)

当我调试请求对象时,我可以看到remoteUser=null.那么如何将值传递给远程用户呢?

spring spring-test mockito spring-test-mvc

3
推荐指数
1
解决办法
4530
查看次数

Spring Security LDAP VS CAS VS OpenID 的区别

在 spring security 中,我知道有不同的模块迎合了不同的用途,在我看到的一些模块中,有 LDAP、CAS 和 OPENID。

根据我的理解

  1. CAS - 它仅用于基于 SSO 的身份验证目的
  2. LDAP - 基于 LDAP 服务器对用户进行身份验证和管理。我对么?
  3. OPENID - 它还基于 OpenID 服务器来验证用户

如果是这样,为什么有些人会使用 CAS 而不是 LDAP?也许是因为不同的可用性?任何人都可以摆脱他们三个之间的区别,为什么一个比其他的更受欢迎?

spring cas ldap spring-mvc spring-security

3
推荐指数
1
解决办法
3151
查看次数

Intellij spring启动应用程序无法在tomcat中运行

这是我的班级

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@Configuration
@ComponentScan
@EnableAutoConfiguration
@SpringBootApplication
public class TomcatIc?nApplication {

    public static void main(String[] args) {
        SpringApplication.run(TomcatIc?nApplication.class, args);
    }
}

@RestController
class GreetingController {

    @RequestMapping("/hello/hi")
    String hello( ) {
        return "Hello, xx!";
    }
}
Run Code Online (Sandbox Code Playgroud)

当我运行应用程序并打开时localhost:8080/hello/hi,我可以看到输出.但是从Edit Configurations,当我在端口添加Tomcat服务器8081作为localhost:8081/hello和Tomcat运行这段时间,它调用的应用程序,并打开网页,但它是空的.

为什么我看不到我的输出?

java spring tomcat intellij-idea spring-boot

3
推荐指数
1
解决办法
6115
查看次数

Spring MVC中的PUT请求

我正在尝试PUT在Spring MVC中编写一个简单的请求方法.我得到以下内容:

@RequestMapping(value = "/users/{id}", method = RequestMethod.PUT) 
public @ResponseBody User updateUser(@PathVariable("id") long id, 
                                     String name, 
                                     String email) {
        User user = repository.findOne(id);
        user.setName(name);
        user.setEmail(email);
        System.out.println(user.toString());
        repository.save(user);
        return user; 
} 
Run Code Online (Sandbox Code Playgroud)

这显然是错误的,因为它返回以下内容:

User{id=1, name='null', email='null'}
Run Code Online (Sandbox Code Playgroud)

我也试过@RequestBody注释,但这也没有帮助.任何想法我在这里做错了将不胜感激.

java spring spring-mvc

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

当用户在Spring Boot应用程序中输入无效的URL时,如何显示自定义404页面?

我在Spring Boot,Spring Security应用程序中使用java config。我已如下配置错误控制器。但是每当我输入无效的URL时,它都会转到error.jsp,该URL 被配置为处理应用程序中的错误:

@Controller
public class AppErrorController implements ErrorController {

    private static final String PATH = "/error";

    @RequestMapping(value = "/pageNotFound", method = { RequestMethod.GET, RequestMethod.POST })
    public String pageNotFound() {
        return "pageNotFound";
    }

    @RequestMapping(value = "/accessDenied", method = { RequestMethod.GET, RequestMethod.POST })
    public String accessDenied() {
        return "accessDenied";
    }

    @RequestMapping(value = PATH)
    public String error() {
        return "error";
    }

    @Override
    public String getErrorPath() {
        return PATH;
    }
}
Run Code Online (Sandbox Code Playgroud)

web.xml

<error-page>
    <error-code>404</error-code>
    <location>/pageNotFound</location>
</error-page>

<error-page>
    <error-code>500</error-code>
    <location>/error</location> …
Run Code Online (Sandbox Code Playgroud)

java spring spring-security spring-boot

3
推荐指数
1
解决办法
9530
查看次数

Spring RestTemplate发送列表获取列表

我想用Spring提供服务RestTemplate,在我的服务方面,代码是这样的:

@PostMapping(path="/savePersonList")
@ResponseBody
public List<Person> generatePersonList(@RequestBody List<Person> person){
    return iPersonRestService.generatePersonList(person);
}
Run Code Online (Sandbox Code Playgroud)

在客户端,如果我使用此代码调用服务:

List<Person> p = (List<Person>) restTemplate.postForObject(url, PersonList, List.class);
Run Code Online (Sandbox Code Playgroud)

我无法使用该p对象List<Person>,它将成为一个LinkedHashList.经过一些研究后,我找到了一个解决方案,说我必须用交换方法调用服务:

ResponseEntity<List<Person>> rateResponse = restTemplate.exchange(url, HttpMethod.POST, personListResult, new ParameterizedTypeReference<List<Person>>() {});
Run Code Online (Sandbox Code Playgroud)

并且使用此解决方案,服务器无法获取对象并引发异常,这是正确的方法吗?

java spring resttemplate

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

Spring Data JPA 在 Spring Boot 应用程序中不使用 AttributeConverter

我有一个 spring boot 应用程序,它AttributeConverter为一个实体指定一个,该实体将枚举从大写转换为标题大小写以存储在数据库中。

我有以下实体:

@Entity
@Table(name = "customerleads")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class CustomerLead implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Enumerated(EnumType.STRING)
    @Column(name = "type")
    @Convert(converter = CustomerLeadTypeConverter.class)
    private CustomerLeadType type = CustomerLeadType.OPEN;
}
Run Code Online (Sandbox Code Playgroud)

以及以下 AttributeConverter 类:

@Converter(autoApply = true)
public class CustomerLeadTypeConverter implements AttributeConverter<CustomerLeadType, String> {

    @Override
    public String convertToDatabaseColumn(CustomerLeadType attribute) {
        switch (attribute) {
            case OPEN:
                return "Open";
            case CLOSED:
                return "Closed";
            case DELETED:
                return "Deleted";
            default:
                throw new IllegalArgumentException("Unknown" + attribute); …
Run Code Online (Sandbox Code Playgroud)

java spring hibernate spring-data-jpa spring-boot

3
推荐指数
1
解决办法
4961
查看次数