我在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) 我HandlerInterceptor
在Spring Boot中使用它来处理常见的请求类型.但是在执行时preHandle
,如果不满足条件,我想将错误状态代码返回给用户.如果我preHandle
在响应中抛出异常将具有所有异常堆栈.如何使用preHandle的响应代码发送自定义正文作为响应
我有一个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 security 中,我知道有不同的模块迎合了不同的用途,在我看到的一些模块中,有 LDAP、CAS 和 OPENID。
根据我的理解
如果是这样,为什么有些人会使用 CAS 而不是 LDAP?也许是因为不同的可用性?任何人都可以摆脱他们三个之间的区别,为什么一个比其他的更受欢迎?
这是我的班级
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运行这段时间,它调用的应用程序,并打开网页,但它是空的.
为什么我看不到我的输出?
我正在尝试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
注释,但这也没有帮助.任何想法我在这里做错了将不胜感激.
我在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) 我想用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)
并且使用此解决方案,服务器无法获取对象并引发异常,这是正确的方法吗?
我有一个 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) spring ×8
java ×6
spring-boot ×4
mongodb ×2
spring-mvc ×2
cas ×1
heroku ×1
hibernate ×1
ldap ×1
mockito ×1
resttemplate ×1
spring-test ×1
tomcat ×1