0

I am trying to send a body in a post request in a springboot application using rest template. Here is the controller:(I removed @RequestBody because I used application/x-www-form-urlencoded header)

    @RestController
@CrossOrigin
@RequestMapping("/api")
public class SentimentParserController {

    @Autowired
    private SentimentParserService sentimentParserService;

  @RequestMapping(value = "/something", method = RequestMethod.POST, consumes="application/x-www-form-urlencoded")
    public ResponseEntity<mcResponse>getTheSentiments(  mcSentimentRequestDTO sentimentRequestDTO){
        return  sentimentParserService.getSentimentsMc(sentimentRequestDTO);


    }
}

I want to send the sentimentRequestDTO object(lang, key, and text) as the body in a post request to get the mcResponse:

public mcResponse parseTheSentiments(String text, Languages lang, String key) throws Exception {

RestTemplate restTemplate = new RestTemplate();
String request = "http://localhost:8080";

mcSentimentRequestDTO mSentiments =new mcSentimentRequestDTO(key,"EN",text);

HttpHeaders headers = new HttpHeaders();
headers.add("content-type", "application/x-www-form-urlencoded");

MultiValueMap<String, String> map= new LinkedMultiValueMap<String, String>();
map.add("key", key);
map.add("txt", text);
map.add("lang", Languages.ENGLISH.toString());
HttpEntity<MultiValueMap<String, String>> request1 = new HttpEntity<MultiValueMap<String, String>>(map, headers);

mcResponse response = restTemplate.postForObject(request, request1 , mcResponse.class );

return response;

}

However, I am getting the following error: 404 null.

Can you please help me? Thanks in advance and here is the service class:

public ResponseEntity<mcResponse> getSentimentsMc(mcSentimentRequestDTO sentimentRequestDTO){
        ResponseEntity<mcResponse> dto = null;

                try {
                    dto = sentimentConverter.getTheSentiments(mcsParser.parseTheSentiments(sentimentRequestDTO.getText(),
                            Languages.ENGLISH, sentimentRequestDTO.getKey()));
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

        return dto;
    }
thecoder
  • 29
  • 5

2 Answers2

0

Looks like variable request should be

String request = "http://localhost:8080/something";

Also if controller class has prefix, this prefix also should be in request. I mean if your class looks like this

@RestController
@RequestMapping("/myApi")
public class CertificateController {
....
@RequestMapping(value = "/something", method = RequestMethod.POST)
    public ResponseEntity<mcResponse>getTheSentiments(  mcSentimentRequestDTO sentimentRequestDTO){
        return  sentimentParserService.getSentimentsMc(sentimentRequestDTO);
}

Then request should be

String request = "http://localhost:8080/myApi/something";
0

It sounds like the controller isn't getting included in the spring context. If you just have an app annotated with @SpringBootApplication, then make sure that your controller is in a package that is the same as or lower than your annotated application.

To check the controller is being picked up you can add the following logging options to your application.properties

logging.level.org.springframework.beans=debug
logging.level.org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping=trace

When your server starts up you should see something like the following in the log

1. To show the controller is in the spring-context

DefaultListableBeanFactory     : Creating shared instance of singleton bean 'sentimentParserController'

2. To show the mapping for the /api/something url

RequestMappingHandlerMapping : Mapped 1 handler method(s) for class SentimentParserController: {public org.springframework.http.ResponseEntity SentimentParserController.getTheSentiments(mcSentimentRequestDTO)={[/api/something],methods=[POST]}}

If you see both of these, then what you say you're doing should work. Just make sure you are sending the request to /api/something and the server is running on port 8080.

pcoates
  • 2,102
  • 1
  • 9
  • 20