相关疑难解决方法(0)

什么是原始类型,为什么我们不应该使用它?

问题:

  • 什么是Java中的原始类型,为什么我经常听说不应该在新代码中使用它们?
  • 如果我们不能使用原始类型,它有什么替代方案,它是如何更好的?

java generics raw-types

617
推荐指数
13
解决办法
20万
查看次数

为Spring RESTful应用程序使用ResponseEntity <T>和@RestController时

我正在使用Spring Framework 4.0.7,以及MVC和Rest

我可以和平地工作:

  • @Controller
  • ResponseEntity<T>

例如:

@Controller
@RequestMapping("/person")
@Profile("responseentity")
public class PersonRestResponseEntityController {
Run Code Online (Sandbox Code Playgroud)

用这个方法(只是为了创建)

@RequestMapping(value="/", method=RequestMethod.POST)
public ResponseEntity<Void> createPerson(@RequestBody Person person, UriComponentsBuilder ucb){
    logger.info("PersonRestResponseEntityController  - createPerson");
    if(person==null)
        logger.error("person is null!!!");
    else
        logger.info("{}", person.toString());

    personMapRepository.savePerson(person);
    HttpHeaders headers = new HttpHeaders();
    headers.add("1", "uno");
    //http://localhost:8080/spring-utility/person/1
    headers.setLocation(ucb.path("/person/{id}").buildAndExpand(person.getId()).toUri());

    return new ResponseEntity<>(headers, HttpStatus.CREATED);
}
Run Code Online (Sandbox Code Playgroud)

返回一些东西

@RequestMapping(value="/{id}", method=RequestMethod.GET)
public ResponseEntity<Person> getPerson(@PathVariable Integer id){
    logger.info("PersonRestResponseEntityController  - getPerson - id: {}", id);
    Person person = personMapRepository.findPerson(id);
    return new ResponseEntity<>(person, HttpStatus.FOUND);
}
Run Code Online (Sandbox Code Playgroud)

工作良好

我可以这样做:

  • @RestController(我知道它与@Controller+ …

spring spring-mvc spring-3 spring-4

153
推荐指数
4
解决办法
22万
查看次数

SonarQube 抱怨使用带有通配符的 ResponseEntity

我使用 SpringBoot 进行 REST Web 服务开发,使用 SonarQube 进行静态分析。

我的应用程序中有一些端点,如下所示:

@PostMapping
ResponseEntity<?> addSomething(@RequestBody Some object) {
    // some code there
    return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
Run Code Online (Sandbox Code Playgroud)

SonarQube 抱怨使用带有通配符的 ResponseEntity,向我报告了一个严重问题“通用通配符类型不应在返回参数中使用”

我想知道是否应该在 SonarQube 中禁用此验证,或者为这些情况提供不同的返回类型。

你怎么看待这件事?

spring-mvc spring-boot sonarqube

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