0

I'm frightfully new to Spring and Java but I'm trying to consume some code for some rule validations in Easy Rules but I can't quite figure it out.

    @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);
    }
}

So I'm trying something like:

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);

The JSON data I'm trying to pull back is modeled like this but I need to validate to make sure that record equals one of the values from enums[] json array

{
  "field": "a",
  "descriptor": "blah",
  "enums": [
    {
      "value": "h",
      "description": "blah"
    },
    {
      "value": "e",
  "description": "blah"
},
{
  "value": "l",
  "description": "blah"
},
{
  "value": "p",
  "description": "blah"
}
  ]
}
lion
  • 1
  • 5

2 Answers2

1

What is the problem that you are seeing is it just not matching? If so it could be because you are using == instead of String.equals. Try modifying your code to:

return record.equals(value);

See Java String.equals versus == for more.

Community
  • 1
  • 1
Patrick Bray
  • 552
  • 2
  • 7
  • 20
  • No more, looking to see if I'm using RestTemplate correctly, when debugging I'm getting a 'source not found' and that probably has something to do with the service but I'm wondering if I'm properly taking the called json and transferring it correctly to be compared in the check at the end. – lion May 16 '17 at 02:00
  • In that case if you change to the following it should work: `String uri = "http://localhost:9000/author/field?field={field}"` – Patrick Bray May 16 '17 at 07:22
  • hm, still getting same thing. – lion May 16 '17 at 18:31
  • So you have changed the last part of the url to field?field={field} do your model classes have public getter and setters? Can you include them here what is the exception or response you are getting from the server? – Patrick Bray May 16 '17 at 20:08
1

Can you change String uri = "http://localhost:9000"

and missed the path variable name field it should be like author/field?field={field} as per your controller description.

Zico
  • 2,349
  • 2
  • 22
  • 25
  • yeah, normally I'm trying to hit a running service but I'm just running it locally now for code completion. – lion May 16 '17 at 18:32
  • Oh that is my fault, I just didn't type it out when into the question. In my code it has the http part. I will edit, thanks for pointing that out. – lion May 16 '17 at 19:02
  • 1
    Can you please provide the stack trace of exception? And which line you get? – Zico May 16 '17 at 19:36
  • And please describe the Enum class – Zico May 17 '17 at 08:54