我是Spring和Java的新手,但是我试图在Easy Rules中为一些规则验证使用一些代码,但我无法弄明白。
@RequestMapping(method = {RequestMethod.GET}, value = "author/field", produces= MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody ResponseEntity<Enum> getField(@RequestParam(value="field", required=true) String field){
Enum enum = mongoService.findByField(field);
if(enum == null){
return new ResponseEntity<Enum>(HttpStatus.NO_CONTENT);
}else{
return new ResponseEntity<Enum>(enum,HttpStatus.OK);
}
}
所以我正在尝试这样的事情:
import com.mongoservice.Enum
import com.mongoservice.Enums
RestTemplate restTemplate = new RestTemplate();
String uri = "http://localhost:9000";
//This is my confusion
List<Enums> response = restTemplate.getForObject(uri +
"/author/field?={field}", Enum.class,"a").getEnums();
String value = response.getValue().toString().trim();
//this is the record i'm checking against that is pulling a specific string value and what i'm expecting
String record = "a";
return (value == record);
我试图撤回的JSON数据是这样建模的,但我需要验证以确保记录等于enums [] json array
中的一个值{
"field": "a",
"descriptor": "blah",
"enums": [
{
"value": "h",
"description": "blah"
},
{
"value": "e",
"description": "blah"
},
{
"value": "l",
"description": "blah"
},
{
"value": "p",
"description": "blah"
}
]
}
答案 0 :(得分:1)
你看到的问题是它不匹配?如果是这样,可能是因为你使用的是==而不是String.equals。尝试将代码修改为:
return record.equals(value);
有关详情,请参阅Java String.equals versus ==。
答案 1 :(得分:1)
您可以更改String uri = "http://localhost:9000"
并错过了路径变量名称field
,根据您的控制器说明,它应该像author/field?field={field}
一样。