我正在使用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+ …我使用 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 ×2
generics ×1
java ×1
raw-types ×1
sonarqube ×1
spring ×1
spring-3 ×1
spring-4 ×1
spring-boot ×1