#Getmapping gives 404 - spring

My Code:
#RestController("/productivityapi")
public class ProductivityController {
private final IProdDAO prodDAO;
#Autowired
public ProductivityController(IProdDAO injectedBean) {
this.prodDAO = injectedBean;
}
#PostMapping("/search")
public ResponseEntity searchRecords(#RequestParam String date, String shift) {
return ResponseEntity.ok(prodDAO.getProdDetails(date, shift));
}
#PostMapping("/averages")
public ResponseEntity searchAverages() {
return ResponseEntity.ok(prodDAO.searchAverages());
}
#GetMapping("/")
public String healthCheck() {
return "ok";
}
#GetMapping("/test")
public ResponseEntity test() {
return ResponseEntity.ok("Hello from productivity API!");
}
}
The posts are working but the #GetMapping I always get 404 error? The posts work fine, I'm at a loss with this, can anybody see if I have done anything obviously wrong?
Many thanks

Related

spring resttemplate request object not mapping to rest controller

i have below resttempalte which invokes rest controller of another service..
#Override
public ResponseEntity<String> callRestAPI(APIReqDataMO apiReqDataMO) {
String apiURL = URIGenerator.getAPIURL(apiReqDataMO);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
HttpEntity<?> request = new HttpEntity<>(apiReqDataMO.getRequestObject(), headers);
ResponseEntity<String> httpRes = restTemplate.postForEntity(apiURL, request, String.class);
return httpRes;
}
and in my service i have controller, which consumes above request..
#RequestMapping(value = "/targetService/createUser", method = RequestMethod.POST, consumes = "application/json")
public String fuzzerServiceAge(UserMO userMO) {
System.out.println("---------------------age is -------------------------" + userMO.getAge());
if (userMO.getAge() > 0) {
System.out.println("error age greater than 0 ");
return "invalid user age";
} else if (userMO.getAge() == 0) {
return "invalid user age";
}
return "user added successfully";
}
when i try my test.. the age which i am pushing through rest template is not getting mapped..and i am getting age as 0 always in my system.out.. what could be wrong in my code... and is there anything missing from configuration perspective..
EDIT -
public class APIReqDataMO {
private String restAPIURL;
private Object[] pathParam;
private Object[] requestParam;
private String requestType;
private String paramType;
private Object requestObject;
public String getParamType() {
return paramType;
}
public void setParamType(String paramType) {
this.paramType = paramType;
}
public String getRequestType() {
return requestType;
}
public void setRequestType(String requestType) {
this.requestType = requestType;
}
public Object getRequestObject() {
return requestObject;
}
public void setRequestObject(Object requestObject) {
this.requestObject = requestObject;
}
public String getRestAPIURL() {
return restAPIURL;
}
public void setRestAPIURL(String restAPIURL) {
this.restAPIURL = restAPIURL;
}
public Object[] getPathParam() {
return pathParam;
}
public void setPathParam(Object[] pathParam) {
this.pathParam = pathParam;
}
public Object[] getRequestParam() {
return requestParam;
}
public void setRequestParam(Object[] requestParam) {
this.requestParam = requestParam;
}
}
controller
#PostMapping("/targetService/createUser")
public String fuzzerServiceAge(UserMO userMO) {
System.out.println("--------------------- age is -------------------------" + userMO.getAge());
if (userMO.getAge() > 0) {
// return ResponseEntity.ok("Hello World!");
} else if (userMO.getAge() == 0) {
System.out.println(" it is else block");
// return ResponseEntity.badRequest().build();
}
// return ResponseEntity.ok("user added successfully!");
return "user added successfully";
}
usermo
public class UserMO {
#JsonProperty("name")
private String name;
#JsonProperty("age")
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Issue
There is an issue in API implementation. You are creating POST API and when the user will invoke this API by passing UserMO in the request body then mapping won't happen because the #RequestBody annotation is missing.
#PostMapping("/targetService/createUser")
public String fuzzerServiceAge(UserMO userMO) {
System.out.println("--------------------- age is -------------------------" + userMO.getAge());
if (userMO.getAge() > 0) {
// return ResponseEntity.ok("Hello World!");
} else if (userMO.getAge() == 0) {
System.out.println(" it is else block");
// return ResponseEntity.badRequest().build();
}
// return ResponseEntity.ok("user added successfully!");
return "user added successfully";
}
Solution
If you are using #RestController annotation on top of the controller class then add #RequestBody annotation before UserMO userMO and try again.
Like this
#PostMapping("/targetService/createUser")
public String fuzzerServiceAge(#RequestBody UserMO userMO) {
//logic
}
if you are using #Controller annotation on top of the controller class then add #ResponseBody annotation on top of method fuzzerServiceAge() and #RequestBody annotation before UserMO userMO and try again.
Like this
#PostMapping("/targetService/createUser")
#ResponseBody
public String fuzzerServiceAge(#RequestBody UserMO userMO) {
//logic
}

What is the best way to return different types of ResponseEntity in Spring-Boot

I would like to return two different response for a spring boot rest API.
I should not be using <?> wild card as i get the sonar issue "Generic wildcard types should not be used in return types"
My code:
#GetMapping(path = {"/v1/{type}"}, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<?> method(#PathVariable(value = "type") boolean type) {
boolean b = type;// some logic
if (b) {
Success result=new Success();
result.setSuccess("Yes");
return new ResponseEntity<>(result,HttpStatus.OK);
}
else {
Error result=new Error();
result.setError("No");
return new ResponseEntity<>(result,HttpStatus.CONFLICT); //appropriate error code
}
}
Any idea how to handle this situation.
Update:
public interface MyResponse{
public Success getSuccessObj();
public Error getErrorObj();
}
#Service
public class Success implements MyResponse {
public Error getErrorObj(){
return null;
}
public Success getSuccessObj(){
Success s=new Success();
return s;
}
#Service
public class Error implements MyResponse {
public Error getErrorObj(){
Error e=new Error();
return e;
}
public Success getSuccessObj(){
return null;
}
Not claiming to be "the best way", but one approach can be:
Introduce:
package com.my.package;
public interface MyResponseI { //if Error, Success (and others) have more "in common", you can also introduce an (abstract) class (with fields, methods, etc.)!
}
"Implement"/Extend:
public class Success implements com.my.package.MyResponseI { //everything else can stay}
as
public class Error implements com.my.package.MyResponseI { //everything else can stay}
Use as Response Type:
#...
public ResponseEntity<com.my.package.MyResponseI> ...
(on client side distinguish).
..and in "your domain" (error, success, ...), you are free to use any "tweaks" of a object oriented design.
Useful links/entries:
https://stackoverflow.blog/2020/03/02/best-practices-for-rest-api-design/
https://swagger.io/resources/articles/best-practices-in-api-design/
https://www.google.com/search?q=rest+api+design
, but also
https://www.google.com/search?q=object+oriented+design
and https://www.google.com/search?q=domain+driven+design
This should work
I tried the snippet below by myself and it worked for me:
#GetMapping("/testresponse/{id}")
public ResponseEntity<?> testMyResponse(#PathVariable("id") int id)
{
if(id==1)
return ResponseEntity.ok(new Success());
else return new ResponseEntity<>(new Error(), HttpStatus.CONFLICT);
}
public class Success {
private String msg = "Success";
public String getMsg() {
return msg;
}
}
public class Error {
private String msg = "Error";
public String getMsg() {
return msg;
}
}
EDIT: The solution as below doesn't work
You should also define an interface for both Success and Error classes. Let say the interface MyResponse
And then change your method declaration, it would look like this
public ResponseEntity<MyResponse> method(#PathVariable(value = "type") boolean type)
If so, the return statement, could be:
return new ResponseEntity<>(result, HttpStatus.OK);
Or
//for status 200 OK
return ResponseEntity.ok(result);

While testing api in postman getting error 404

Model Class
public class SimpleResponse {
private String message;
private long id;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
}
Controller class
#RestController
public class WebController {
// #RequestParam indicates that the endpoint /sample will have one Query
// parameter called name.
#RequestMapping("/sample")
public SimpleResponse sample(#RequestParam(value = "name", defaultValue = "Kabir") String name) {
SimpleResponse sm = new SimpleResponse();
sm.setId(1);
sm.setMessage("This message is for Kabir"+name);
return sm;
}
}
While developing a Spring Boot Rest Api i am geeting 404 error.Unable to find the Error.Can anyone help me to resolve this issue?
Above code works fine, did you check your application logs?
Check whether if your application.properties file contains a property as server.servlet.context-path. If it does, then append that to your URL after the domain.
http://localhost:8888/<append_here>/sample?name=Kabir

415--Unsupported Media Type in Spring

I am getting unsupported mediatype error.
My User Profile class looks like this
Class UserProfile{
private int age;
private String name,
private String currenecy;
}
And this is the method in controller
#RequestMapping(value = "/Create", method=RequestMethod.POST,consumes=MediaType.APPLICATION_JSON_VALUE, produces=MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<UserProfileResponse> createUserProfile(#RequestBody UserProfile userProfile)
{
UserProfileResponse userProfileResponse = new UserProfileResponse();
int id = createUserProfileData(userProfile)
userProfileResponse.setId(id);
return new ResponseEntity<UserProfileResponse>(userProfileResponse,HTTPStatus.OK);
}
I am trying to send the request through POSTMAN but getting
Error 415--Unsupported Media Type
My Request in POstman looks like this
Content-Type:application/json
Accept:application/json
Method is : POST
{
"age":28,
"name":"Sam",
"currency": "INR"
}
Suggest me what I am missing?
Don't forget to select "JSON" format, filled in arbitrary JSON string in the textarea.
Also use either Accept or Content-type at a time.
If that doesn't work then can you check like below by removing consumes and adding headers manually.
#RequestMapping(value = "/Create", method=RequestMethod.POST, headers = "Accept=application/json",produces=MediaType.APPLICATION_JSON_VALUE)
I could see the response coming back with your code. I am deliberately returning the same object just to test the connectivity. Following is my code:
#RequestMapping(value = "/create", method= RequestMethod.POST,consumes= MediaType.APPLICATION_JSON_VALUE, produces=MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<UserProfile> createUserProfile(#RequestBody UserProfile userProfile)
{
System.out.println("Got request");
return new ResponseEntity<>(userProfile, HttpStatus.OK);
}
Used getter and setter in UserProfile
public class UserProfile {
private int age;
private String name;
private String currenecy;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCurrenecy() {
return currenecy;
}
public void setCurrenecy(String currenecy) {
this.currenecy = currenecy;
}
}
Finally after after spending some time.. I figured out why it was not working.
In my java based spring configuration file I missed "#EnableWebMvc".
After adding it, my problem got resolved.
#Configuration
**#EnableWebMvc** // This annotation was missing.
#ComponentScan(basePackages="com.hemant.*")
public class TestConfiguration {}

NullPointerException TopLevelTransaction.markAsRollbackOnly Neo4J-Boot for findAll() method

I am trying to follow Josh Long - Philip Sorst example in order to perform Angular SPA-Rest Authentication and CRUD actions using Neo4J as db (excellent work. Thanks a lot guys). But I am stuck at a very early stage and I suspect it's not my fault. Please help Neo4J-Angular-Spring lovers. My code can be found here and it is very easy to run just clone and give mvn spring-boot:run
Now the problem is that I get the following exception only for the findAll() method of the GraphRepository.
Caused by: java.lang.NullPointerException: null
at org.neo4j.kernel.TopLevelTransaction.markAsRollbackOnly(TopLevelTransaction.java:93)
... 88 common frames omitted
and I will replicate some of my code:
Neo4JConfig.java
#Configuration
#EnableNeo4jRepositories(basePackages = "demo.repository.neo4j")
public class Neo4JConfig extends Neo4jConfiguration {
public Neo4JConfig() {
setBasePackage("demo.model.neo4j");
}
#Bean(destroyMethod = "shutdown")
public GraphDatabaseService graphDatabaseService() {
return new GraphDatabaseFactory().newEmbeddedDatabase("data/demo.db");
}
#Bean
public Neo4jTemplate neo4jTemplate() {
return new Neo4jTemplate(graphDatabaseService());
}
}
NewsEntry.java
#NodeEntity
public class NewsEntry {
#GraphId
private Long id;
private String content;
public NewsEntry() {}
public NewsEntry(String b) {
this.content = b;
}
public Long getId() {
return this.id;
}
public String getContent() {
return this.content;
}
public void setId(Long id) {
this.id = id;
}
public void setContent(String content) {
this.content = content;
}
}
NewsEntryRepository.java
public interface NewsEntryRepository extends GraphRepository<NewsEntry> {
}
NewsEntryController.java
#RestController
class NewsController {
#Autowired
private NewsEntryRepository newsEntryRepository;
#RequestMapping("/news")
List<NewsEntry> entries() {
List<NewsEntry> list = new ArrayList<NewsEntry>();
Iterable<NewsEntry> results = newsEntryRepository.findAll();
for (NewsEntry r : results) {
list.add(r);
}
return list;
}
#RequestMapping(value = "/news/{id}", method = RequestMethod.DELETE)
void remove(#PathVariable Long id) {
this.newsEntryRepository.delete(id);
return;
}
#RequestMapping(value = "/news/{id}", method = RequestMethod.GET)
NewsEntry entry(#PathVariable Long id) {
return this.newsEntryRepository.findOne(id);
}
#RequestMapping(value = "/news/{id}", method = RequestMethod.POST)
NewsEntry update(#PathVariable Long id, #RequestBody NewsEntry news) {
NewsEntry old = this.newsEntryRepository.findOne(id);
old = news;
return this.newsEntryRepository.save(old);
}
#RequestMapping(value = "/news", method = RequestMethod.POST)
NewsEntry add(#RequestBody NewsEntry news) {
this.newsEntryRepository.save(new NewsEntry(news.getContent()));
return news;
}
}
OK Solution inspired from here https://github.com/mstahv/bootiful-neo4j-with-vaadin/blob/master/src/main/java/org/vaadin/neo4j/AppService.java. He is implementing a #Service annotated with #Transactional before using the repository in the controller.
You shouldn't have to override:
#Bean
public Neo4jTemplate neo4jTemplate() {
return new Neo4jTemplate(graphDatabaseService());
}
And can you try to add the annotation:
#EnableTransactionManagement
to your config?
Something is telling me that the solution is hiding somewhere here. I will give it a go and let you know.
OK This works only if you change the version of Spring-data-neo4j to 2.3.3.RELEASE. If you use the latest version you get the same problem as above. I think I should open an issue.
But that's not the solution as I would like to use Neo4J Server Community 2.0.3 to open the graph for visualization afterwards. Also I do not understand this solution beanfactories, injects instead of autowired???. However I will make another branch for this solution in my github repo.

Resources