How to create Spring Boot controller












0















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!










share|improve this question

























  • 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 service

    – TinyOS
    Nov 16 '18 at 0:43











  • simply this is the request that my controller must handle, does not return anything it's void

    – AlejoDev
    Nov 16 '18 at 0:46
















0















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!










share|improve this question

























  • 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 service

    – TinyOS
    Nov 16 '18 at 0:43











  • simply this is the request that my controller must handle, does not return anything it's void

    – AlejoDev
    Nov 16 '18 at 0:46














0












0








0








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!










share|improve this question
















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!







java spring-boot controller






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 2:22







AlejoDev

















asked Nov 16 '18 at 0:16









AlejoDevAlejoDev

384524




384524













  • 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 service

    – TinyOS
    Nov 16 '18 at 0:43











  • simply this is the request that my controller must handle, does not return anything it's void

    – AlejoDev
    Nov 16 '18 at 0:46



















  • 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 service

    – TinyOS
    Nov 16 '18 at 0:43











  • simply this is the request that my controller must handle, does not return anything it's void

    – AlejoDev
    Nov 16 '18 at 0:46

















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 service

– TinyOS
Nov 16 '18 at 0:43





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 service

– TinyOS
Nov 16 '18 at 0:43













simply this is the request that my controller must handle, does not return anything it's void

– AlejoDev
Nov 16 '18 at 0:46





simply this is the request that my controller must handle, does not return anything it's void

– AlejoDev
Nov 16 '18 at 0:46












1 Answer
1






active

oldest

votes


















0














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






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53329664%2fhow-to-create-spring-boot-controller%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














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






    share|improve this answer






























      0














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






      share|improve this answer




























        0












        0








        0







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






        share|improve this answer















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







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 16 '18 at 1:17

























        answered Nov 16 '18 at 1:12









        TinyOSTinyOS

        99011130




        99011130
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53329664%2fhow-to-create-spring-boot-controller%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Florida Star v. B. J. F.

            Danny Elfman

            Lugert, Oklahoma