• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

Spring Boot Rest client

 
Ranch Hand
Posts: 502
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am getting the following exception

The client code is

The controller is

In the debugger the controller is working fine.
Initially I had the same problem returning an ArrayList, so I changed the code to return an array. Currently I want to return a String; I will represent every object by a JSON string and I will write the JSON strings in the return String separated by new lines '\n'.  I still don't see a way to convert Java objects to JSON strings and how to construct Java objects from JSON Strings.
 
Saloon Keeper
Posts: 14681
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should be using @RestController for web API controllers. Spring will automatically convert the type returned from your controller methods to the appropriate JSON response body, given that you've set the media types on your controller correctly.

Note that you're essentially performing an SQL injection attack on your own service. This is a REALLY bad idea. Attackers will use it to break your application or harm your users.

If you're looking to perform CRUD applications on a repository through a REST controller, you might want to use Spring Data REST instead.
 
Sheriff
Posts: 22716
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Note that you're essentially performing an SQL injection attack on your own service. This is a REALLY bad idea. Attackers will use it to break your application or harm your users.


I warned Alejandro about that in https://coderanch.com/t/733980/frameworks/org-springframework-web-client-HttpServerErrorException#3413974.
 
Alejandro Barrero
Ranch Hand
Posts: 502
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using @RestController didn't make any difference.
 
Rob Spoor
Sheriff
Posts: 22716
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This 500 comes from your server. That means that the server is throwing an exception, that Spring Boot turns into a 500. What do the server logs say?
 
Bartender
Posts: 2265
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may find this Stackoverflow thread helpful:
https://stackoverflow.com/questions/11178937/spring-resttemplate-gives-500-error-but-same-url-credentails-works-in-restcli
 
reply
    Bookmark Topic Watch Topic
  • New Topic