0
votes

I have a spring boot application that needs a controller that can handle the following request:

The request is sent by another service through the Post method..

Headers

accept-encoding: gzip,deflate

user-agent: Apache-HttpClient/4.3.6 (java 1.5)

connection: Keep-Alive

host: webhook.site

content-type: application/x-www-form-urlencoded

content-length: 558

Query strings:(empty)

Form values

BillNumber: 41492032464

BillValue: 600000.0

Description: Description

I have this controller, but my application returns an HTTP Error 406:

@RequestMapping(value = "/bills", method = RequestMethod.POST, headers = "Accept=application/x-www-form-urlencoded")
    @ResponseBody
    @Transactional
    public void createBill(UriComponentsBuilder uriComponentsBuilder, final HttpServletRequest request,
            final HttpServletResponse response) throws IOException {
}

How should this controller be implemented in my spring boot app?

Many Thanks!

1
Can you more explain ! Do want to khnow how to call a service rest ? or you want to create a service rest witch create a bill ? if yes so can you give your object that you want to send to your serviceTinyOS
simply this is the request that my controller must handle, does not return anything it's voidAlejoDev

1 Answers

0
votes

It's not very clear for me but if you use spring boot you can of course create a controller , service and repository or dao. Indeed, your controller will call your service witch will call the repository.

Let's suppose that you have a client witch call your api.

So the call will look like :

// Suppose that is a spring boot project

Class A {

@Autowired 
RestTemplate restTemplate;

public void create(){

    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
    headers.setContentType((MediaType.APPLICATION_JSON));
    headers.add("X-yourCustom-context", "yourCustom");

    UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(your_service_url)
                    .queryParam("params1", "value".queryParam("params2", value2));
    HttpEntity<?> entity = new HttpEntity<>(headers);

    restTemplate.exchange(builder.build().encode().toUri(), HttpMethod.POST, entity, null); // if you want to return an objectr you put it rather than the null

}

}

Th service api :

@RestController
public class YourController {
    @Autowired
    private YourService service;

    @Autowired
    private ObjectMapper objectMapper;


    @PostMapping(value = "/bills")
    //@ResponseBody if you do not return any think you can not use it
    // @CrossOrigin if you want to call your reste from an external project like javascript or angular
    //@Transactional you can put it on the top of your service methode
    public void createBill(@RequestParam(value = "params1", required = true) String params1, 
    @RequestParam(value = "params2", required = true) String params2,
    @RequestHeader(value = "X-yourCustom-context", required = true) String yourContxt) throws IOException {

    // You can then convert your x-context to an object that you have already by using objectMapper.readValue
    // Finaly you call you service to create the bill and you passe as params what you get fron the client 
}
}

I hope it meets your needs :)